0

I've the following code:

arr = []; 
var item={
  id: '2',
  step: 'step2',
  label: 'Banana',
  price: '19$'
}
arr.push(item);

How can I check if this particular item exists in JS. For example, how can I check that id = 2 exists ?

And how can I remove it from the array if found ?

Thanks.

  • You would need to loop over the array and check the object for the id. And that is NOT a multidimensional array. – epascarello Mar 05 '16 at 01:23
  • Here is a JSFiddle showing you a function that will do it: https://jsfiddle.net/e625qjgr/ – kemiller2002 Mar 05 '16 at 01:24
  • Not sure why this question was closed. The linked question with answer while works is from 2011 and since then a lot of things have changed. Now we have an `Array.find` method that takes care of this scenario. See my jsfiddle link: https://jsfiddle.net/xp3tdovs/ There is a polyfill for IE provided by mozilla https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find – TeaCoder Mar 05 '16 at 01:32
  • @TeaCode: You can still provide an answer to that duplicate. – Lye Fish Mar 05 '16 at 01:34
  • 1
    @LyeFish added answer to the linked question. – TeaCoder Mar 05 '16 at 01:40

0 Answers0