Open utterances-bot opened 3 years ago
Wow Super. its great to learn all these new options to merge array. I was not aware of "[].concat" until I saw this post. Thanks for writing this informative article.
Girls are also superheroes or villains :). I love your blog
const heroes = ['Batman', 'Superman'];
const villains = ['Joker', 'Bane'];
const heroines = ['Superwoman', 'Captain Marvel'];
heroes.push(...[...villains, ...heroines]);
its a shallow copy tho... if u have nested objects/arrays they will still be referenced in memory... Maybe next article about the proper way to mutate nested objects??? that things trips me up everytime man! love your articles btw!
Girls are also superheroes or villains :). I love your blog
const heroes = ['Batman', 'Superman']; const villains = ['Joker', 'Bane']; const heroines = ['Superwoman', 'Captain Marvel']; heroes.push(...[...villains, ...heroines]);
Looks good @MaryJJ :).
The "best" option, at least this is what I use all the time, is the immutable
merge of arrays using the spread operator, but I also want to add this extra option ways to merge arrays in JS:
const heroes = ['Batman', 'Superman'];
const villains = ['Joker', 'Bane'];
Array.prototype.push.apply(heroes, villains);
heroes; // ['Batman', 'Superman', 'Joker', 'Bane']
Thanks for the great lesson! Best wishes to you!
@1ucasdev Thanks. 👍
This is a great article! Thanks @panzerdp
3 Ways to Merge Arrays in JavaScript
How to merge arrays in JavaScript using spread operator, array.concat() and array.push().
https://dmitripavlutin.com/javascript-merge-arrays/