I want to reaturn true/false if there is some duplicates on object array.
arr = [
{ nr:10, name: 'aba' },
{ nr:11, name: 'cba' },
{ nr:10, name: 'aba' }
]
arr2 = [
{ year:2020, city: 'Aaa' },
{ year:2010, city: 'Bbb' },
{ year:2020, city: 'Aaa' }
]
I found many solutions for one value but what if we want to check the whole object. Do I have to do foreach?
const result: T[] = [];
for (const item of array) {
if (!result.includes(item)) {
result.push(item);
}
}
return result;
you can use JSON.stringify and map
and some
array methods.
let arr = [
{ nr:10, name: 'aba' },
{ nr:11, name: 'cba' },
{ nr:10, name: 'aba' }
]
let valuesStringify = arr2.map(x => JSON.stringify(x));
let duplicate = valuesStringify.some((item, i) => valuesStringify.indexOf(item) !== i)
console.log(duplicate) //true