I have some array like this
const array1= ['A', 'B', 'C', 'D'];
const array2= [
{category: 'photos', code: 'A', fileName: '1664725062718.jpg', size: 120306},
{category: 'photos', code: 'F', fileName: '1664725062718.jpg', size: 120306},
{category: 'photos', code: 'K', fileName: '1664725062718.jpg', size: 120306},
];
I need some function that will check if any array member form array1 exist in code property of array2 and return true or false?
Something like this, of course this is just not working example?
array1.some(value => array2.includes(value))
I think a clean way is to do the "opposite" and use some
on array2
, destructuring code
from each object, and then checking if the code is in the first array.
const array1= ['A', 'B', 'C', 'D']; const array2= [ {category: 'photos', code: 'A', fileName: '1664725062718.jpg', size: 120306}, {category: 'photos', code: 'F', fileName: '1664725062718.jpg', size: 120306}, {category: 'photos', code: 'K', fileName: '1664725062718.jpg', size: 120306}, {category: 'photos', code: null, fileName: '1664725062718.jpg', size: 120306}, ]; // short-circuiting makes it shorter: // console.log(array2.some(({ code }) => code && array1.includes(code))); console.log(array2.some(({ code }) => code? array1.includes(code): false));
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.