简体   繁体   中英

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. If both front and back object is present set document to true or if only front is present set document to 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.find(({type, frontBack})=> type == 'document' && frontBack == 'front')

And you can check if data has a frontBack:'back' with this code:

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

If both expressions return an object, you have both.
If any of them return undefined , you don't.

You can use some method of Array object, which returns true if some of the elements pass the given test.

 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.

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. 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:

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

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.

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