how to convert json values in comma separated string using javascript

后端 未结 8 783
情话喂你
情话喂你 2021-01-18 06:29

I have following JSON string :

相关标签:
8条回答
  • 2021-01-18 07:24

    You can also look into .reduce and create a string manually

    var d = [{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}]
    
    var location_id_str = d.reduce(function(p, c) {
      return p ? p + ',' + c.location_id : c.location_id
    },'');
    
    console.log(location_id_str)

    0 讨论(0)
  • 2021-01-18 07:25

    var arr = [{"name":"Marine Lines","location_id":3},{"name":"Ghatkopar","location_id":2}];
    
    var location_array = [];
    for( var i = 0; i < arr.length; i++ )
    {
      location_array.push( arr[i].location_id );
    }//for
    
    var location_string = location_array.join(",");
    console.log(location_string);

    Note: You may need to use JSON.parse() if the arr is in string format initially.

    0 讨论(0)
提交回复
热议问题