-5

Here i want to replace price in data1 to price from data2, and it updated if the id is same.

Is it possible to do that without nested loop?

var data1 = [{
          "id": "56e641d4864e5b780bb992c6",
          "price": "1000",
          "quantity": "1",
          "name": "def"
        },
        {
          "id": "56e65504a323ee0812e511f2",
          "price": "2000",
          "quantity": "2",
          "name": "abc"
        }]

var data2 = [{
            "id": "56e65504a323ee0812e511f2",
            "price": "800",
            "name": "abc"
        },
        {
            "id": "56e641d4864e5b780bb992c6",
            "price": "900",
            "name": "def"
        }]

the result that i want looks like this

result = [{
              "id": "56e641d4864e5b780bb992c6",
              "price": "900",
              "quantity": "1",
              "name": "def"
            },
            {
              "id": "56e65504a323ee0812e511f2",
              "price": "800",
              "quantity": "2",
              "name": "abc"
            }]
-2

Here is an indirect answer (javascript) to manipulate directly your field in javascript object from jason

var text = '{ "employees" : [' +
'{ "firstName":"John" , "lastName":"Doe" },' +
'{ "firstName":"Anna" , "lastName":"Smith" },' +
'{ "firstName":"Peter" , "lastName":"Jones" } ]}';
var obj = JSON.parse(text);

writeln(obj.employees[1].firstName + obj.employees[2].firstName);

var temp=obj.employees[1].firstName;

obj.employees[1].firstName = obj.employees[2].firstName;
obj.employees[2].firstName = temp;

obj.employees[1].firstName + obj.employees[2].firstName;

Not the answer you're looking for? Browse other questions tagged or ask your own question.