-1

I have a JSON object as show here:

[
{
    "ID": "1",
    "Country": "India",
    "Value1": "100",
    "Value2": "200"
},
{
    "ID": "2",
    "Country": "China",
    "Value1": "230",
    "Value2": "800"
}
]

This is the result that I am looking for:

[
        ['India', 100],
        ['China', 230],
    ]

I tried using Jquery $.map function but was unable to get what exactly I wanted. Any pointers would be appreciated.

RJP
  • 385
  • 5
  • 19
  • 1
    *"I tried using Jquery $.map"* ... show what you tried. The idea here is you post code that isn't working and people help fix it. Then you learn from mistakes. The idea is not to have people write code from scratch and do all the work for you – charlietfl Jun 14 '16 at 23:08
  • Possible duplicate of [From an array of objects, extract value of a property as array](http://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) – 1983 Jun 15 '16 at 05:55

4 Answers4

3

You can use map function :

var arr = [
{
    "ID": "1",
    "Country": "India",
    "Value1": "100",
    "Value2": "200"
},
{
    "ID": "2",
    "Country": "China",
    "Value1": "230",
    "Value2": "800"
}
];

console.log(arr.map(country=>[country.Country, country.Value1]));
Jose Hermosilla Rodrigo
  • 3,513
  • 6
  • 22
  • 38
0

This is the code that got me what I wanted:

var testJSON=[];
          for(i=0;i<data.length;i++){
          testJSON.push([data[i].Country,data[i].Value1])
          }
RJP
  • 385
  • 5
  • 19
0

Try this :

var arr = [
{
    "ID": "1",
    "Country": "India",
    "Value1": "100",
    "Value2": "200"
},
{
    "ID": "2",
    "Country": "China",
    "Value1": "230",
    "Value2": "800"
}
];

var res = arr.map(function(item) {
 return [item.Country,item.Value1];
})

console.log(res);

Output : enter image description here

Working fiddle : https://jsfiddle.net/h6kxvcys/

Debug Diva
  • 26,058
  • 13
  • 70
  • 123
-1

as i understand your question you want to create make object inside object.here i modify rohith's json structure .try this .i wish you will got correct answer from this.

Try this:

var arr = [{
"ID": {
    "name":"Allan" ,
    "sid":"1"
},
"Country": "India",
"Value1": "100",
"Value2": "200"},{
"ID":  {
    "name":"Brian" ,
    "sid":"2"
},
"Country": "China",
"Value1": "230",
"Value2": "800"}];

Out put:

enter image description here