我想知道如何联系不可变的数组。假设我从数组 list = [4,1]
开始,然后我从 Action 响应中接收数组,如下所示 items = [5,2,6]
.如何连接结果为 [4,1,5,2,6]
的数组并且该操作是不可变的。
奖金 :如何覆盖具有相同 id 的项目(不可变方式)?让我们想象一下我们在 store 中的数组 books=[{'id':1, 'title': 'Cool story'}, {'id':2, 'title': 'Bad story'}]
.其他需要覆盖书籍的数组(最后从 API 同步)otherArray = [{'id':3, 'title': 'Super story'}, {'id':1, 'title': 'Very cool story'}]
.所以结果应该是[{'id':2, 'title': 'Bad story'}], {'id':3, 'title': 'Super story'}, {'id':1, 'title': 'Very cool story'}]
最佳答案
与 ES6 您可以使用解构:
const array1 = ["Banana","Apple"];
const array2 = ["Pineapple", "Peach"];
const array3 = [...array1, ...array2];
关于javascript - 如何连接数组不可变的方式JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41209675/