Hi i have an object like so,
const obj = {
id: '1',
itemData: [
"type1",
"type2",
"type3",
],
}
sometimes this itemData array can be empty array.
now i have to check if itemData has "type1" or "type2". if so then should return true if not false.
i have tried like below,
const isDisabled = obj.itemData.map(e => return e === "type1" || e==="type2")
but this doesnt work. could someone help me with this. thanks.
You can use includes with some
const obj = {
id: '1',
itemData: [
"type1",
"type2",
"type3",
],
}
console.log(!obj.itemData.length || obj.itemData.some(el => ['type1', 'type2'].includes(el)));
The
some()method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn't modify the array.
The
includes()method determines whether an array includes a certain value among its entries