That's not related to angular2/typescript, my answer works with ES6.
Use uniq
function from lodash (https://lodash.com/docs/4.17.4#uniq) on items from your sources array like this:
const arrA = [1, 2, 3, 4];
const arrB = [3, 4, 5, 6];
const arr = _.uniq([...arrA, ...arrB])
// [1, 2, 3, 4, 5, 6]
And for nested object, use uniqBy
(https://lodash.com/docs/4.17.4#uniqBy) or uniqWith
.
const arrA = [{id: 1, n: 'e'}, {id: 2, n: 'z'}];
const arrB = [{id: 2, n: 'z'}, {id: 3, n: 'c'}];
const arr = _.uniqBy([...arrA, ...arrB], 'id')
// [{id: 1, n: 'e'}, {id: 2, n: 'z'}, {id: 3, n: 'c'}]
But you need a unique identifier (id
here) to know when duplicates.
[EDIT] In case, you don't have a unique identifier and want to use whole objects to deduplicate, you can do it like this:
const arrA = [{a: 'a'}, {b: 'b'}];
const arrB = [{b: 'b'}, {c: 'c'}];
const arr = _.uniqBy([...arrA, ...arrB], JSON.stringify)
// [{a: 'a'}, {b: 'b'}, {c: 'c'}]
It will stringify all your object to check if same string values, so remember it could be costly on huge objects/array. That's a better practise to have a unique identifier.