简体   繁体   English

如何在 javascript 中检查数组是否包含这两个对象

[英]How to how check an array contains these two objects in javascript

I want check whether document front and back object is present in an array.我想检查文档正面和背面 object 是否存在于数组中。 If both front and back object is present set document to true or if only front is present set document to false如果正面和背面 object 都存在,则将文档设置为 true,或者如果仅存在正面,则将文档设置为 false

const [document, setDocument] = useState('');

let data = [
  { id: 1, type:'document', frontBack:'front'},
  { id: 2, type:'pan', frontBack:'front'},
  { id: 3, type:'profile', frontBack:'front'},
  { id: 4, type:'document', frontBack:'back'},
];

You can check if data has a frontBack:'front' with this code:您可以使用以下代码检查data是否具有frontBack:'front'

data.find(({type, frontBack})=> type == 'document' && frontBack == 'front')

And you can check if data has a frontBack:'back' with this code:您可以使用以下代码检查data是否有frontBack:'back'

data.find(({type, frontBack})=> type == 'document' && frontBack == 'back')

If both expressions return an object, you have both.如果两个表达式都返回一个 object,那么你就有了。
If any of them return undefined , you don't.如果它们中的任何一个返回undefined ,你就不会。

You can use some method of Array object, which returns true if some of the elements pass the given test.您可以使用数组 object 的some方法,如果某些元素通过给定的测试,则返回true

 let data = [{ id: 1, type: 'document', frontBack: 'front' }, { id: 2, type: 'pan', frontBack: 'front' }, { id: 3, type: 'profile', frontBack: 'front' }, { id: 4, type: 'document', frontBack: 'back' }, ]; let out = data.some(d => d.type == "document" && d.frontBack == "front") && data.some(d => d.type == "document" && d.frontBack == "back"); console.log(out);

 let data = [ { id: 1, type:'document', frontBack:'front'}, { id: 2, type:'pan', frontBack:'front'}, { id: 3, type:'profile', frontBack:'front'}, { id: 4, type:'document', frontBack:'back'}, ]; const values = ['front', 'back']; // values to check const filteredData = data?.filter((record) => record?.type === 'document')?.map(filteredRec => filteredRec?.frontBack); // type wise filtered data console.log(values.every((value) => filteredData.includes(value))); // if this returns `true` then set `setDocument` to `true`

There are multiple approaches to solving this problem, but you could make use of .some() method to check if the criteria mentioned above are satisfied or not.有多种方法可以解决此问题,但您可以使用.some()方法来检查是否满足上述条件。

setDocument(data.some(item=>item.frontBack === 'front') && data.some(item=>item.frontBack === 'back'))

.some() returns true if one or more items in the iterable returns true, so we are checking if both front and back exists, and using the conditional && we can say if both the items exist or not and set the state accordingly.如果可迭代对象中的一个或多个项目返回 true, .some() back true,因此我们正在检查front是否都存在,并使用条件&&我们可以判断这两个项目是否存在并相应地设置 state。 You can also make the code more understandable by extracting the logical operation result into a separate variable, like so:您还可以通过将逻辑运算结果提取到单独的变量中来使代码更易于理解,如下所示:

const res = data.some(item=>item.frontBack === 'front') && data.some(item=>item.frontBack === 'back')

setDocument(res)

I tried to write it out in as straightforward a way as possible.我试图以尽可能简单的方式写出来。 I wasn't sure which variable you meant to set, so I used one called setDoc:我不确定您要设置哪个变量,所以我使用了一个名为 setDoc 的变量:

 //const [document, setDocument] = useState(''); let data = [ { id: 1, type:'document', frontBack:'front'}, { id: 2, type:'pan', frontBack:'front'}, { id: 3, type:'profile', frontBack:'front'}, { id: 4, type:'document', frontBack:'back'}, ]; let setDoc, front, back = false data.forEach(obj => { if(obj.type === 'document' && obj.frontBack === 'front') { front = true } if(obj.type === 'document' && obj.frontBack === 'back') { back = true } }) setDoc = front && back console.log(setDoc)

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

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