我有以下情況:道場mixin兩個陣列如何?
我有一個變量數組這樣的:
[
{
id: "foo",
value: "bar"
},
{
id: "baz",
value: "buz"
}
]
和我有一個缺省值陣列是這樣的:
[
{
id: "foo",
value: "default"
},
{
id: "bar",
value: "default"
},
{
id: "baz",
value: "default"
}
]
我想這兩個陣列比較並通過將缺少的默認值添加到第一個數組中將它們混合在一起。
這就是我想要的輸出:
[
{
id: "foo",
value: "bar" // keep unchanged because it was already there
},
{
id: "bar",
value: "default" // use the default because it was not in there
},
{
id: "baz",
value: "buz" // also keep because it was already in the first array
}
]
什麼是實現這一目標的最好方法是什麼?謝謝你的幫助!
大答案+1 –