[英]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.