2

Is there a method like includes that only searches for an element in an array but allows you to see the elements that are being iterating on?

for example: I have the following array:

const arr = [{ color: 'red' },{ color: 'blue' },{ color: 'pink' },{ color: 'green' }]

I want to check if the arr holds an element with the value of pink: so I thought if there's something like this in JavaScript:

arr.includes( (el, index)=> el.color == 'pink' )

which then returns a boolean, either true or false.

Is there a method like that in JavaScript? if not, what about lodash?

3
  • 4
    arr.some(el => el.color === "pink") returns true or false Commented Apr 30, 2022 at 18:04
  • I marked my question as a duplicate, but I think that it should be separated in a stand alone thread, because it's a general question and not related to a particular thing to achieve, How can I unmark it from being duplicated? Commented Apr 30, 2022 at 18:10
  • 1
    You can't directly. You can edit your question, explain why you think it is different and then mark the "Submit for review" checkbox. After that the community will review and vote to reopen when they agree with it. I don't give it much chance though. It answers the question perfectly after all. Commented Apr 30, 2022 at 18:17

1 Answer 1

4

I think you want find:

if (arr.find(el => el.color === "pink")) console.log("it's there");
Sign up to request clarification or add additional context in comments.

1 Comment

some would be better, because it returns a boolean.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.