3

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 ?

4
  • 3
    Possible duplicate of Converting JSON Object into Javascript array Commented Aug 29, 2017 at 4:34
  • Possible duplicate of Converting a JS object to an array Commented Aug 29, 2017 at 4:47
  • Your expected JSON is invalid. Commented Aug 29, 2017 at 5:06
  • "data" :{ [ 1, "Alex", 20 ] } - this looks like bad syntax, you probably need to learn basics first. you probably need data: [ 1, "Alex", 20 ] Commented Aug 29, 2017 at 9:18

3 Answers 3

9

You can simply try on followings:

var arr = Object.keys(obj).map(function(k) { return obj[k] });
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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.

4 Comments

thanks for you answer but I try in filename.js it's return $ is not defined. I wrong ?
@jane You forgot to add jQuery library file. Add <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> in the head section
I use node.js and I try var jquery = require('jquery'); it's return $ is not defined again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.