Here is my javascript object-
var test=[
[
{book:"A"},
{book:"B"}
]
]
In this object if I want to check whether an object with value "C" exists or not how can I do that?
You could use a nested Array#some
and check agains the value.
var test = [[{ book: 'A' }, { book: 'B' }]], check = 'c'; console.log(test.some(a => a.some(({ book }) => book === check))); // false console.log(test.some(a => a.some(({ book }) => book === 'A'))); // true
My attempt would be:
let hasC = false;
for (let arr of test) {
const obj = arr.find(b => b.book === 'C')
if(obj) {
hasC = true;
break;
}
}
console.log('does test has a book "C"? ', hasC);
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.