1

I'm using Charts.js, I wonder how to convert a string of comma separated values into data array.

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):

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

data response

I need to convert this response data into a var like this:

320.30, 100.00, 65.89, 65.89, 65.89, 244.47, 244.46 ...

EDIT 2: Request data was used with:

$.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:

enter image description here

Ivan
  • 1,221
  • 2
  • 21
  • 43

3 Answers3

3

use String.split() to split based on a delimiter, so

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

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.

the if() block makes sure there is a value in commission before setting it. this prevents undefined value from showing up.

tdc
  • 5,174
  • 12
  • 53
  • 102
  • so you're trying to format it as a string? so concatenate each value together with a comma seperating? – tdc Nov 11 '15 at 17:59
  • yes, concantenating each value togheter with a comma separating, also edited my question again, look EDIT 2 – Ivan Nov 11 '15 at 18:05
  • It works but how to get rid of "undefined" screenshot of console: http://i.imgur.com/T5jZ4MJ.png – Ivan Nov 11 '15 at 18:11
  • the very first item in `data.orders` (the `[0]` index), does not have a value for commission. Please see my edit – tdc Nov 11 '15 at 18:17
  • If you want your answer gets accepted, please fix how to avoid this bad loop. Thanks! – Ivan Nov 11 '15 at 18:26
  • 1
    @Ivan console.log outside of the loop. you're seeing each addition because the log is inside the loop. – tdc Nov 11 '15 at 18:38
1

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,

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>
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
  • It doesn't help me because how I am supposed to make arrayOfValues at first place if my response data returns value each line in loop? – Ivan Nov 11 '15 at 18:13
  • @Ivan Used an array instead of string which would allow you to change the delimiter in the join with a slight change where it is needed. – Selvakumar Arumugam Nov 11 '15 at 18:35
  • @Ivan Are you pushing "comments" instead of "commision". That image shows the items getting added to arrow is not commision but an empty string in that object. – Selvakumar Arumugam Nov 11 '15 at 19:05
  • @Ivan Yes, I see that.. but What I am saying is that there is no way for you to get empty commas (, , , , , ) as you had shown in the above comment unless you are pushing a different element instead of commission. Check what var you are pushing at line `commisions.push(item.commision);` in your code when you got that console output. – Selvakumar Arumugam Nov 11 '15 at 19:10
  • Did you notice your var data, their properties have no quotes? It should be {"orders": [{"commision": 320.30}, ... ] } Try it – Ivan Nov 11 '15 at 19:12
  • @Ivan You don't need quotes for property name in JS however JSON response always serializes with quotes as it is required in other languages. – Selvakumar Arumugam Nov 11 '15 at 19:17
  • Still got empty values, I totally copied your snippet in my repo, I swear – Ivan Nov 11 '15 at 19:21
  • @Ivan Could you post what you had copied in your question? including the `console.log`. Thanks. – Selvakumar Arumugam Nov 11 '15 at 19:25
  • Added, look my question where starts @Vega – Ivan Nov 11 '15 at 19:32
0

Use string method split.

E.x. you have a string.

    let string = "split,a,string";
    let arrayOfStrings = string.split(',');

arrayOfStrings will be ["split", "a", "string"].