kaniosrn-j
3/13/2019 - 11:42 AM

How to append array in an array, or concat

// ES5
Array.prototype.push.apply(existingArr, newArr);

let arr1 = [1, 2, 3, "prince"];
arr1.concat([3, 2, 1, "prince"].filter(function(el) {
    return arr1[1, 2, 3].indexOf(el) === -1;
}));
// [1, 2, 3, "prince"]

var a = [1, 2, 3], b = [101, 2, 1, 10];
var c = a.concat(b.filter(function (item) {
    return a.indexOf(item) < 0;
}));
// d is [1,2,3,101,10]

// ES6
array1.push(...array2) // => don't remove duplication 
[...array1,...array2] //   =>  don't remove duplication 
[...new Set([...array1 ,...array2])]; //   => remove duplication