[英]How to convert a string of comma separated values into data array?
我正在使用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
编辑:这些答案,我不是在寻找,因为我不能获得一个带有单独的值的变量,我需要将其转换。
这是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;
});
@维加
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(', '));
});
控制台的结果:
使用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,控制台
编辑
根据您的修改,执行以下操作:
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
。 这样可以防止显示未定义的值。
即兴与数组。 维护阵列将增加灵活性,以便以后需要时进行修改。
//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;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="result1"></div>
如果我正确地理解了您编辑过的帖子:您可以使用 .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>
使用字符串方法split 。
例如,您有一个字符串。
let string = "split,a,string";
let arrayOfStrings = string.split(',');
arrayOfStrings将为[“ split”,“ a”,“ string”]。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.