我試圖找到答案但未成功。如何將新陣列添加到另一個陣列的特定對象
JSON數據的樣本,可以說將它命名爲mainArray:
[{
"id": 4455,
"key1": 44,
"key2": 55
},
{
"id": 1122,
"key1": 11,
"key2": 22
}]
數據需要mainArray要添加的樣品nestedArray:
[{
"nestedkey0": 'Value0',
"nestedkey1": "Value1",
"nestedkey2": "Value2"
},
{
"nestedkey0": "Value3",
"nestedkey1": "Value4",
"nestedkey2": "Value5"
}]
我們必須檢查「id」鍵(在mainArray)是否等於「specificId」變量:
var specificId = 4455;
如果他們是平等的 - 我們需要添加nestedArray成mainArray對象與對象列表中的新鑰匙,所以返回的結果應該是這樣的:
[{
"id": 4455,
"newKey": [{
"nestedkey0": 'Value0',
"nestedkey1": "Value1",
"nestedkey2": "Value2"
},
{
"nestedkey0": "Value3",
"nestedkey1": "Value4",
"nestedkey2": "Value5"
}];
"key1": 44,
"key2": 55
},
{
"id": 1122,
"key1": 11,
"key2": 22
}];
下面是Plunker上的代碼示例: https://plnkr.co/edit/YJ3lTbleKrBJBaJm7VwU?p=preview
我有一種感覺,應該不難,但我不是很有經驗d以angularjs解決此任務。 非常感謝您的回答!
更新
在Plunker更新代碼尼娜肖爾茨(鏈接以上)
而且從另一種解決方案太平
的答案http://jsbin.com/xaxagusuma/edit?html,js,console,output
感謝所有您的幫助!
你有什麼試圖解決這個問題?向我們展示您嘗試過的代碼。 –
我是angularjs和平面javascript的新成員,所以我試着在這裏找到解決方案,但是我做不到。我也嘗試過push()方法,但它增加了** nestedArray **,就像** mainArray **中的對象的兄弟一樣。 – Vlad