我使用的是Charts.js,我想知道如何将一串逗号分隔值转换为数据数组。
有一个响应数据,它会返回(我已编码格式化):
100.00
65.89
244.47
244.46
314.99
320.30
314.99
319.63
然后我将此数据保存为AJAX响应之后的var(使用jQuery):
var dataComissions;
$.each(data.orders, function(i, item) {
var dataComissions = data.orders[i].commission;
});
这个数据有每个值的换行符,我需要这样的例外结果作为一个带逗号的字符串:
100.00, 65.89, 244.47, 244.46, 314.99, 320.30, 314.99, 319.63
编辑:这些我不看的答案,因为我无法获得一个带有分隔值的var,我需要转换它。
这是我得到的console.log
我需要将此响应数据转换为如下所示的var:
320.30, 100.00, 65.89, 65.89, 65.89, 244.47, 244.46 ...
编辑2:请求数据用于:
$.getJSON( "orders/list", function( data, status ) {
}).done(function(data) {
$.each(data.orders, function(i, item) {
console.warn(data.orders[i].commission); //This is from screenshot
var dataComissions = data.orders[i].commission;
});
@Vega
var commisions = [];
$.getJSON( "phrapi/orders/list", function( data, status ) {
}).done(function(data) {
// console.warn(data.orders[0].created_at);
var commisions = [];
$.each(data.orders, function(i, item) {
commisions.push(item.commision);
});
console.log(commisions.join(', '));
});
控制台的结果:
答案 0 :(得分:3)
使用String.split()基于分隔符进行拆分,所以
var string_of_values = '100.00, 65.89, 244.47, 244.46, 314.99, 320.30, 314.99, 319.63';
var arr = string_of_values.split(', '); // this says, "split my string into an array, dividing each element by ', '. Note the whitespace is included here.
console.log(arr); // outputs: ["100.00", "65.89", "244.47", "244.46", "314.99", "320.30", "314.99", "319.63"]
http://jsbin.com/rebirigeyu/edit?html,js,console
修改强>
根据您的编辑,执行以下操作:
var dataComissions = ''; // stores the result of concat
$.getJSON( "orders/list", function( data, status ) {
}).done(function(data) {
$.each(data.orders, function(i, item) {
if (data.orders[i].commission) {
dataComissions += data.orders[i].commission+', '; // add to our store
}
});
console.warn(dataComissions); //This is from screenshot
问题是您每次都在重新设置dataComissions
,而不是追加您的价值。
if()
块确保在设置之前commission
中有值。这可以防止显示未定义的值。
答案 1 :(得分:1)
使用数组即兴创作。维护一个数组会增加后期修改的灵活性。
private System.Windows.Forms.DataGridView myNewGrid; // Declare a grid for this form
private List<BattleShipRow> battleShipGrid; // Declare this here so that you can use it later to manipulate the cell contents
private void Form1_Load(object sender, EventArgs e)
{
myNewGrid = new System.Windows.Forms.DataGridView();
((System.ComponentModel.ISupportInitialize)(myNewGrid)).BeginInit();
this.SuspendLayout();
myNewGrid.Parent = this; // You have to set the parent manually so that the grid is displayed on the form
myNewGrid.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
myNewGrid.Location = new System.Drawing.Point(10, 10); // You will need to calculate this postion based on your other controls.
myNewGrid.Name = "myNewGrid";
myNewGrid.Size = new System.Drawing.Size(400, 400); // You said you need the grid to be 12x12. You can change the size here.
myNewGrid.TabIndex = 0;
myNewGrid.ColumnHeadersVisible = false; // You could turn this back on if you wanted, but this hides the headers that would say, "Cell1, Cell2...."
myNewGrid.RowHeadersVisible = false;
myNewGrid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
myNewGrid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
myNewGrid.CellClick += MyNewGrid_CellClick; // Set up an event handler for CellClick. You handle this in the MyNewGrid_CellClick method, below
((System.ComponentModel.ISupportInitialize)(myNewGrid)).EndInit();
this.ResumeLayout(false);
myNewGrid.Visible = true;
LoadGridData();
}
public class BattleShipRow
{
public string Cell1 { get; set; }
public string Cell2 { get; set; }
public string Cell3 { get; set; }
public string Cell4 { get; set; }
public string Cell5 { get; set; }
public string Cell6 { get; set; }
public string Cell7 { get; set; }
public string Cell8 { get; set; }
public string Cell9 { get; set; }
public string Cell10 { get; set; }
public string Cell11 { get; set; }
public string Cell12 { get; set; }
}
private void LoadGridData()
{
battleShipGrid = new List<BattleShipRow>();
for (var i = 0; i < 12; i++)
{
battleShipGrid.Add(new BattleShipRow());
}
myNewGrid.DataSource = battleShipGrid;
}
private void MyNewGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
throw new NotImplementedException();
}
//your ajax respose ignoring the other fields
var data = {orders: [{commision: 320.30}, {commision: 100.00}, {commision: 65.89}, {commision: 65.89}, {commision: 65.89}, {commision: 244.47}, {commision: 244.46}]};
var commisions = [];
$.each(data.orders, function(i, item) {
commisions.push(item.commision);
});
//use commisions.join(', ') to join the array to a single string seperated by a comma and a space
document.getElementById('result').innerHTML = commisions.join(', ');
基于您的第二次编辑:根据输入数据重新格式化。您可能需要将其设置为循环外的字符串对象。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result"></div>
//your ajax respose ignoring the other fields
var data = {orders: [{commision: 320.30}, {commision: 100.00}, {commision: 65.89}, {commision: 65.89}, {commision: 65.89}, {commision: 244.47}, {commision: 244.46}]};
var resultString = '';
$.each(data.orders, function(i, item) {
resultString += item.commision + ((i == (data.orders.length - 1))? "":", ");
});
document.getElementById('result1').innerHTML = resultString;
如果我正确理解了您编辑的帖子:您可以使用{,1}来替换响应字符串中的换行符,使用“{”(逗号和空格),
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result1"></div>
.replace
答案 2 :(得分:0)
使用字符串方法split。
E.x。你有一个字符串。
<input id="input" type="text" onkeyup="exampleFunction()" />
arrayOfStrings将是[&#34; split&#34;,&#34; a&#34;,&#34; string&#34;]。