简体   繁体   中英

How to convert JSON to Array in Javascript

I want to convert JSON to Array and I return value by : console.log(data);

value is :

 [{ "data" : [ [object] ] }, [{ "data" : [ [object] , [object] ] } 

so, I converted to JSON by:

console.log(JSON.stringify(data, null, "    "));

value is :

 [ { "data" : [ { "month" : 1, "name" : "Alex", "sum" : 20 } ] }, { "data" : [ { "month" : 2, "name" : "Zara", "sum" : 18 }, { "month" : 2, "name" : "Zara", "sum" : 19 } ] } ] 

I want convert to Array :

 { "data" : { [ 1, "Alex", 20 ] } }, { "data" : { [ 2, "Zara", 18 ] }, { [ 2, "Zara", 19 ] } } 

How to convert ?

您可以简单地尝试以下方法:

var arr = Object.keys(obj).map(function(k) { return obj[k] });

The syntax of your expected output is incorrect as you cant have object without key value pair.

you can have your output as

{
  "data" : [ [ 1, "Alex", 20 ]  ]
},
{
  "data" : [[ 2, "Zara", 18 ]  ,
           [ 2, "Zara", 19 ]  ]
}

here is the solution considering above output

 var inputArr = [ { "data" : [ { "month" : 1, "name" : "Alex", "sum" : 20 } ] }, { "data" : [ { "month" : 2, "name" : "Zara", "sum" : 18 }, { "month" : 2, "name" : "Zara", "sum" : 19 } ] } ]; inputArr.forEach(function(item){ for (var i=0; i< item.data.length; i++){ var myArr = []; myArr.push(item.data[i].month); myArr.push(item.data[i].name); myArr.push(item.data[i].sum); item.data[i] = myArr; } }) console.log(JSON.stringify(inputArr)); 

NOTE: Solution can be simplified if you are Ok to use ES6 in your code.

Here you go with a solution (with jQuery ) https://jsfiddle.net/mhhqj1nc/

 var data = [ { "data" : [ { "month" : 1, "name" : "Alex", "sum" : 20 } ] }, { "data" : [ { "month" : 2, "name" : "Zara", "sum" : 18 }, { "month" : 2, "name" : "Zara", "sum" : 19 } ] }]; var newdata = []; $.each(data, function(i, v){ newdata.push({data: []}); var temp = []; $.each(v.data, function(k, val){ var keys = Object.keys(v.data[k]); $.each(keys, function(j){ temp.push(v.data[k][keys[j]]); }); newdata[i].data.push(temp); temp = []; }); }); console.log(JSON.stringify(newdata)); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

Your expected output is invalid .

Expected output should be

[{
  "data": [1, "Alex", 20]
 }, {
  "data": [
    [2, "Zara", 18],
    [2, "Zara", 19]
  ]
}]

Here you go with a solution (with vanilla JavaScript ) https://jsfiddle.net/mhhqj1nc/1/

 var data = [ { "data" : [ { "month" : 1, "name" : "Alex", "sum" : 20 } ] }, { "data" : [ { "month" : 2, "name" : "Zara", "sum" : 18 }, { "month" : 2, "name" : "Zara", "sum" : 19 } ] }]; var newdata = []; for(var i=0; i<data.length; i++){ newdata.push({data: []}); for(var j=0; j<data[i].data.length; j++){ var keys = Object.keys(data[i].data[j]); var temp = []; for(var k in keys){ temp.push(data[i].data[j][keys[k]]); } newdata[i].data.push(temp); } } console.log(JSON.stringify(newdata)); 

Hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM