I have this array with 3 of size
[
'decdbaf8-db89-4d4b-973a-e8edab55927c',
'c553c2f7-eff6-476e-b718-2814a7fa5435',
'8717092f-5820-474b-9dd9-165e2649180f'
]
and this array with size equals 2:
[
{
id: 'c553c2f7-eff6-476e-b718-2814a7fa5435',
name: 'test',
codReference: '15422aa',
category_id: '7a026a80-f3d2-462a-ad5d-598e9eabf693',
created_at: 2020-10-29T22:23:35.928Z,
updated_at: 2020-10-29T22:23:35.928Z,
deleted_at: null
},
{
id: 'decdbaf8-db89-4d4b-973a-e8edab55927c',
name: 'test',
codReference: '2',
category_id: '7a026a80-f3d2-462a-ad5d-598e9eabf693',
created_at: 2020-10-29T22:23:37.784Z,
updated_at: 2020-10-29T22:23:37.784Z,
deleted_at: null
}
]
I know that if I were to get only one item that is not contained in the array, I would just use: indexOf (value)> -1;
but how can I compare the two arrays and get only the values of the first array that are not contained in the second array
Something like this perhaps (combination of filter and some)
const first = [ 'decdbaf8-db89-4d4b-973a-e8edab55927c', 'c553c2f7-eff6-476e-b718-2814a7fa5435', '8717092f-5820-474b-9dd9-165e2649180f' ]; const second = [{ id: 'c553c2f7-eff6-476e-b718-2814a7fa5435', name: 'test', codReference: '15422aa', category_id: '7a026a80-f3d2-462a-ad5d-598e9eabf693', deleted_at: null }, { id: 'decdbaf8-db89-4d4b-973a-e8edab55927c', name: 'test', codReference: '2', category_id: '7a026a80-f3d2-462a-ad5d-598e9eabf693', deleted_at: null } ] const result = first.filter(x => !second.some(y => y.id === x)); console.log(result)
You could use a filter. Here's a working solution:
var arr1 = [ 'decdbaf8-db89-4d4b-973a-e8edab55927c', 'c553c2f7-eff6-476e-b718-2814a7fa5435', '8717092f-5820-474b-9dd9-165e2649180f' ] var arr2 = [ { id: 'c553c2f7-eff6-476e-b718-2814a7fa5435', name: 'test', codReference: '15422aa', category_id: '7a026a80-f3d2-462a-ad5d-598e9eabf693', created_at: '2020-10-29T22:23:35.928Z', updated_at: '2020-10-29T22:23:35.928Z', deleted_at: null }, { id: 'decdbaf8-db89-4d4b-973a-e8edab55927c', name: 'test', codReference: '2', category_id: '7a026a80-f3d2-462a-ad5d-598e9eabf693', created_at: '2020-10-29T22:23:37.784Z', updated_at: '2020-10-29T22:23:37.784Z', deleted_at: null } ] var arr = arr2.map(_ => _.id); var res = arr1.filter( function(n) { return !this.has(n) }, new Set(arr) ); console.log(res);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.