[英]How to convert a string of comma separated values into data array?
I'm using Charts.js, I wonder how to convert a string of comma separated values into data array. 我正在使用Charts.js,不知道如何将用逗号分隔的字符串转换成数据数组。
There a response data, it would return (I already coded for formatting): 那里有一个响应数据,它将返回(我已经为格式化编码):
100.00
65.89
244.47
244.46
314.99
320.30
314.99
319.63
Then I save this data as var after of AJAX response (with jQuery): 然后我将这些数据保存为AJAX响应之后的var(使用jQuery):
var dataComissions;
$.each(data.orders, function(i, item) {
var dataComissions = data.orders[i].commission;
});
This data has line break each value, I need excepted result like this as one string with commas: 此数据每个值都有一个换行符,我需要将这样的例外结果作为一个字符串用逗号分隔:
100.00, 65.89, 244.47, 244.46, 314.99, 320.30, 314.99, 319.63
EDIT: These answers that I'm not looking, because I can't get one var with separated values, I need to convert it. 编辑:这些答案,我不是在寻找,因为我不能获得一个带有单独的值的变量,我需要将其转换。
This is console.log what i got 这是console.log我得到的
I need to convert this response data into a var like this: 我需要将此响应数据转换成这样的var:
320.30, 100.00, 65.89, 65.89, 65.89, 244.47, 244.46 ...
EDIT 2: Request data was used with: 编辑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(', '));
});
result of console: 控制台的结果:
use String.split() to split based on a delimiter, so 使用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 http://jsbin.com/rebirigeyu/edit?html,js,控制台
EDIT 编辑
based on your edit, do this: 根据您的修改,执行以下操作:
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
The problem is you were re-setting dataComissions
each time, instead of appending your value. 问题是您每次都在重新设置
dataComissions
,而不是附加值。
the if()
block makes sure there is a value in commission
before setting it. if()
块可确保在设置值之前先进行commission
。 this prevents undefined value from showing up. 这样可以防止显示未定义的值。
Improvised with an array. 即兴与数组。 Maintaining in an array would add the flexibility to modify later if needed.
维护阵列将增加灵活性,以便以后需要时进行修改。
//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>
Based on your second edit: Reformatted based on the input data. 根据您的第二次编辑:根据输入数据重新格式化。 You may need to set it to an string object outside the loop.
您可能需要将其设置为循环外的字符串对象。
//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;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="result1"></div>
If I understood your edited post correctly: You could just replace the line break in the response string with a ", " (comma and a space) using .replace
like below,如果我正确地理解了您编辑过的帖子:您可以使用 .replace
将响应字符串中的换行符替换为“,”(逗号和空格),如下所示,
var stringWithLineBreaks = document.getElementById("ajaxResponse").value; var formattedString = stringWithLineBreaks.replace(/\\n/g, ", "); console.log(formattedString);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="ajaxResponse">100.00 65.89 244.47 244.46 314.99 320.30 314.99 319.63</textarea>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.