-2

I have JSON as

var newJSON = [{"key":"India","value":"72"},{"key":"India","value":"27"},{"key":"Pakistan","value":"90"},{"key":"Zimbamwe","value":"88"},{"key":"India","value":"100"},{"key":"Pakistan","value":"172"}]

I want desired result as

[{"key":"India","value":"199"},{"key":"Pakistan","value":"262"},{"key":"Zimbamwe","value":"88"}]

Please help me with this

  • `{ "India", 200 }` is not valid syntax and there is no such kind of object in JavaScript. –  Jun 07 '15 at 07:14

2 Answers2

0

are you sure you want them as {"india","20"}??

or is it {"india":"20"} //this is recommended

then you can do json["india"] or json[0]

Update:

var yourObj={key:'india', value:72};
var newObj = {};
newObj[yourObj.key] = yourObj.value;
newObj["Austria"]=100; // adds as a new list object
Ruchan
  • 3,124
  • 7
  • 36
  • 72
0

You could use

var jsondata =[];

        var firstvalue = {country:"India",number:"200"};
        jsondata.push(firstvalue);

        var secondvalue = {country:"Australia",number: "210"};
        jsondata.push(secondvalue);
        for(var i =0; i< jsondata.length;i++){
            console.log(jsondata[i].country);
        }
jameshwart lopez
  • 2,993
  • 6
  • 35
  • 65