0

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?

Normal
  • 1,616
  • 15
  • 39
  • 4
    `arr.some(el => el.color === "pink")` returns true or false – bill.gates Apr 30 '22 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? – Normal Apr 30 '22 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. – Ivar Apr 30 '22 at 18:17

1 Answers1

3

I think you want find:

if (arr.find(el => el.color === "pink")) console.log("it's there");
Blunt Jackson
  • 616
  • 4
  • 17