alp*_*iii 2 javascript immutability reactjs
我想知道如何联系不可变的数组.让我们想象一下我从数组开始list = [4,1],然后我从动作响应中接收数组items = [5,2,6].我如何得到结果的并发数组,[4,1,5,2,6]并且该操作不可变.
额外奖励:如何覆盖具有相同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'}]
使用ES6,您可以使用解构:
const array1 = ["Banana","Apple"];
const array2 = ["Pineapple", "Peach"];
const array3 = [...array1, ...array2];
Run Code Online (Sandbox Code Playgroud)