我想知道如何联系不可变的数组。假设我从数组开始list = [4,1],然后像这样从动作响应中接收数组items = [5,2,6]。如何将结果确定为数组[4,1,5,2,6]且该操作不可更改。
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'}]
books=[{'id':1, 'title': 'Cool story'}, {'id':2, 'title': 'Bad story'}]
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'}]
Javascript没有不可变的类型。
这听起来像你实际上问来连接阵列没有 变异 现有实例。
正如文档中明确指出的那样,该concat()方法可以做到这一点。
concat()