How do I add new element to JSON object in multiple places referring to an another element using JavaScript?

时间:2017-06-15 10:21:39

标签: javascript json

I have a Json response as follow with multiple repeated arrays and would need to add a new element&value in each array after "bcd" using javascript code.

{
    "id": "123",
    "discount": [
        {
            "abc": xx,
            "bcd": xxx,
             "mc": {
            "dca": "xx",
            },
            "typ": "xx",
            "state": "xx",
            "mns": []
        },

        {
        "abc": xxx,
        "bcd": xxx,
        "dca": "xxx",
        },
     ]

}

expected response:

{
    "id": "123",
    "discount": [
        {
            "abc": xx,
            "bcd": xxx,
            "newelement":new value,
             "mc": {
            "dca": "xx",
            },
            "typ": "xx",
            "state": "xx",
            "mns": []
        },

            {
            "abc": xxx,
            "bcd": xxx,
           "newelement":new value
            "dca": "xxx",
            },
         ]
}

Can anyone please suggest how to do this using JavaScript,

4 个答案:

答案 0 :(得分:1)

You can simply use

obj.discount.forEach(function(x) { x.newelement = "new value"; });

to iterate through your discount array and add a new property to all of its objects.

However, it won't guarantee the order and it actually should not. Even if one JSON serializer provides some way of ordering for properties, another will not.
JSON is not a string and not a text, is it primarily data.
JSON / JS object properties are not ordered - that's the main difference between arrays and objects.

Read more on this topic here: Does JavaScript Guarantee Object Property Order?

In general, you should never rely on order of JSON object properties and should never expect someone to rely on it.

答案 1 :(得分:0)

您可以映射折扣数组,返回包含所需属性的新对象。

这有利于破坏对象引用和可变性,防止整个代码中的任何副作用改变公共对象的属性。

这些模式是等效的,但es6版本目前仅适用于babel编译。



const data = {
  "id": "123",
  "discount": [
    { "abc": 'xx' },
    { "abc": 'xxx' }
  ]
}

// es5
data.discount = data.discount.map(function(x) {
  return Object.assign({}, x, {
    newElement: 'new value'
  })
})

// es6
data.discount = data.discount.map(x => ({
  ...x,
  newElement: 'new value'
}))

console.log(data)




答案 2 :(得分:0)

这是您的问题的解决方案。小提琴https://jsfiddle.net/tt92jkhx/

var test = {     “id”:“123”,     “折扣”:[         {             “abc”:“xx”,             “bcd”:“xx”,              “mc”:{             “dca”:“xx”,             },             “typ”:“xx”,             “州”:“xx”,             “mns”:[]         }      ]      };

 for(var i=0;i<test.discount.length;i++){
   var disc = test.discount[i];
   disc['newElement']='SomeValue';      
 }

 console.log(test);

答案 3 :(得分:0)

您可以使用ES6数组for...of循环将对象的discount数组迭代到数组的每个对象中的add the property

&#13;
&#13;
var jsonObj = {
    "id": "123",
    "discount": [
        {
            "abc": "xx",
            "bcd": "xxx",
             "mc": {
            "dca": "xx",
            },
            "typ": "xx",
            "state": "xx",
            "mns": []
        },
        {
        "abc": "xxx",
        "bcd": "xxx",
        "dca": "xxx",
        },
     ]
};

for (var value of jsonObj.discount) {
  value.newelement = "new value";
}

console.log(jsonObj.discount);
&#13;
&#13;
&#13;

正如yeldar-kurmangaliyev在他的回答中所说,JSON / JS对象属性没有被排序。因此,它不会保证订单。

查看此帖子以获取有关对象属性顺序的更多参考:https://stackoverflow.com/a/5525820/4116300