0

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.

1
  • thats not valid syntax for an arrow function, remove the return --- also see <Array>.includes Commented Jan 23, 2022 at 20:02

1 Answer 1

1

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.

MDN - some

The includes() method determines whether an array includes a certain value among its entries

MDN - includes

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. how can i also set it to true if itemData is empty array?
@stackuser I changed the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.