const values = [
{ name: 'someName1' },
{ name: 'someName2' },
{ name: 'someName3' },
{ name: 'someName1' }
]
const uniqueValues = new Set(values.map(v => v.name));
if (uniqueValues.size < values.length) {
console.log('uniqueValues')
}
const data = [{id: 1, order_id: 1}, {id: 2, order_id: 1}, {id: 3, order_id: 2}]
const duplicates = data.filter((item, index) =>
index !== data.findIndex((v) => v.order_id === item.order_id));
console.log(duplicates)
const values = [{id: 10, name: 'someName1'}, {id: 10, name: 'someName2'}, {id: 11, name:'someName3'}, {id: 12, name: 'someName4'}];
const lookup = values.reduce((a, e) => {
a[e.id] = ++a[e.id] || 0;
return a;
}, {});
console.log(values.filter(e => lookup[e.id]));
Run code snippetHide results
export const findDuplicate = (data: Record<string, any>[]): number[] => {
const duplicates: number[] = data
.filter((item: Record<string, any>, index: number) => index !== data.findIndex((v: Record<string, any>) => v.order_id === item.order_id))
.map((val: Record<string, any>) => val.id)
return duplicates
}
duplicateIds = arr
.map(e => e['id'])
.map((e, i, final) => final.indexOf(e) !== i && i)
.filter(obj=> arr[obj])
.map(e => arr[e]["id"])