2016-03-08 150 views
2

我試圖找到答案但未成功。如何將新陣列添加到另一個陣列的特定對象

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; 

如果他們是平等的 - 我們需要添加nestedArraymainArray對象與對象列表中的新鑰匙,所以返回的結果應該是這樣的:

[{ 
    "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

感謝所有您的幫助!

+0

你有什麼試圖解決這個問題?向我們展示您嘗試過的代碼。 –

+1

我是angularjs和平面javascript的新成員,所以我試着在這裏找到解決方案,但是我做不到。我也嘗試過push()方法,但它增加了** nestedArray **,就像** mainArray **中的對象的兄弟一樣。 – Vlad

回答

2

在普通的JavaScript,你可以使用Array.prototype.some()的迭代,因爲可能短路。

some()方法測試數組中的某個元素是否通過了由提供的函數實現的測試。

var mainArray = [{ "id": 4455, "key1": 44, "key2": 55 }, { "id": 1122, "key1": 11, "key2": 22 }], 
 
    nastedArray = [{ "nastedkey0": 'Value0', "nastedkey1": "Value1", "nastedkey2": "Value2" }, { "nastedkey0": "Value3", "nastedkey1": "Value4", "nastedkey2": "Value5" }], 
 
    specificId = 4455; 
 

 
mainArray.some(function (a) { 
 
    if (a.id === specificId) { 
 
     a.newKey = nastedArray; 
 
     return true; 
 
    } 
 
}); 
 

 
document.write('<pre>' + JSON.stringify(mainArray, 0, 4) + '</pre>');

+2

非常感謝! – Vlad

0

正如你懷疑這不是很難。最難的部分是找到正確的對象來插入nastedArray

var mainArray = [/**/]; 
var nastedArray = [/***/]; 
var index = null; 
var specificId = 4455; 
for (var i = 0; index === null, i < mainArray.length; i += 1) { 
    if (mainArray[i].id === specificId) { 
     index = i; 
    } 
} 
if (index !== null) { 
    mainArray[index].newKey = nastedArray; 
} 
+0

我試過在這裏使用你的答案http://jsbin.com/xaxagusuma/edit?html,js,console,output,但它看起來像有一個語法錯誤。但我想你的解決方案也應該是正確的。 – Vlad

+0

謝謝,修復。 – Halcyon

+0

if(index!== null){mainArray [index] .newKey = nastedArray; }在您的代碼中 - 獲取specificId var的新鍵值。當我將specificId替換爲var nastedArray時 - 它創建了所需的數組。 – Vlad

1

一種替代與Array.prototype.find

的find()方法在數組中返回一個值,如果陣列中的一個元件滿足提供的測試功能。否則返回undefined。

(看看在compatibility table!)

var mainArray = [{ "id": 4455, "key1": 44, "key2": 55 }, { "id": 1122, "key1": 11, "key2": 22 }], 
 
    nastedArray = [{ "nastedkey0": 'Value0', "nastedkey1": "Value1", "nastedkey2": "Value2" }, { "nastedkey0": "Value3", "nastedkey1": "Value4", "nastedkey2": "Value5" }], 
 
    specificId = 4455; 
 

 

 
var item = mainArray.find(x => x.id === specificId); 
 
if (item !== undefined) { 
 
    item.newKey = nastedArray; 
 
} 
 

 
console.log(JSON.stringify(mainArray));

+1

正如你建議我已經看過兼容性表,它看起來像這種方法不支持IE。不幸的是它不適合我。但無論如何 - 謝謝你的回答! – Vlad

+0

是的,它的支持有限,但可以通過頁面上提到的polyfil繞開。然而,這只是另一種選擇:) – Andreas

+1

解決一個問題的不同方法 - 這是一個研究新工具/ lang /等的完美方法... Tnx爲您的解決方案,感謝您的幫助;) – Vlad

0

大約保持簡單:)

for (var i = 0, i < mainArray.length; i++) { 
     if (mainArray[i].id === specificId) { 
      mainArray[i].newKey = nestedArray; 
      break; 
     } 
    } 

將在所有的瀏覽器是什麼。

+0

Omg,它看起來非常好,簡單)) – Vlad

相關問題