我想知道如何联系不可变的数组。让我们想象一下我从数组开始list = [4,1]
,然后我从动作响应中接收数组,如下所示items = [5,2,6]
。如何连接结果为的数组[4,1,5,2,6]
并且该操作是不可改变的。
Bonus: 如何覆盖具有相同 id 的项目(不可变方式)?让我们想象一下我们存储的数组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'}]
With ES6您可以使用解构:
const array1 = ["Banana","Apple"];
const array2 = ["Pineapple", "Peach"];
const array3 = [...array1, ...array2];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)