How to concat arrays immutable way JS

心不动则不痛 提交于 2019-12-01 23:39:59

Javascript does not have immutable types.

It sounds like you're actually asking to concatenate arrays without mutating the existing instances.

As stated clearly in the documentation, the concat() method does that.

With ES6 you can use destructuring:

const array1 = ["Banana","Apple"];
const array2 = ["Pineapple", "Peach"];
const array3 = [...array1, ...array2];

Although as mentioned, the built-in method .concat() will solve your problem, you might wish to look into the Lodash library. In particular, the bonus question could be solved with _.unionBy(otherArray, books, "id").

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!