简体   繁体   English

检查字符串 typescript 的数组中是否存在某些 object 属性

[英]Check if some object property exist in array of string typescript

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?我需要一些 function 来检查array2的代码属性中是否存在任何数组成员形式array1并返回 true 或 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.我认为一个干净的方法是做“相反”并在array2上使用some ,从每个 object 解构code ,然后检查代码是否在第一个数组中。

 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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM