How do you check if an array contains an object javascript?
No need to reinvent the wheel loop, at least not explicitly (using arrow functions, modern browsers only): Show
or, better yet, use some as it allows the browser to stop as soon as one element is found that matches, so it's going to be faster:
or the equivalent (in this case) find:
And you can even get the position of that element by using findIndex:
And if you need compatibility with lousy browsers then your best bet is:
answered Nov 21, 2011 at 19:43
10 2018 edit: This answer is from 2011, before browsers had widely supported array filtering methods and arrow functions. Have a look at CAFxX's answer. There is no "magic" way to check for something in an array without a loop. Even if you use some function, the function itself will use a loop. What you can do is break out of the loop as soon as you find what you're looking for to minimize computational time.
answered Nov 21, 2011 at 19:34
Alex TurpinAlex Turpin 46k23 gold badges111 silver badges144 bronze badges 9 No loop necessary. Three methods that come to mind: Array.prototype.some() This is the most exact answer for your question, i.e. "check if something exists", implying a bool result. This will be true if there are any 'Magenic' objects, false otherwise:
Array.prototype.filter() This will return an array of all 'Magenic' objects, even if there is only one (will return a one-element array):
If you try to coerce this to a boolean, it will not work, as an empty array (no 'Magenic' objects) is still truthy. So just use Array.prototype.find() This will return the first 'Magenic' object (or
This coerces to a boolean okay (any object is truthy, Note: I'm using vendor["Name"] instead of vendor.Name because of the weird casing of the property names. Note 2: No reason to use loose equality (==) instead of strict equality (===) when checking the name. answered Dec 6, 2016 at 9:25
boxtrainboxtrain 2,3181 gold badge10 silver badges18 bronze badges 2 The accepted answer still works but now we have an ECMAScript 6 native methods Array.some Use
Quoting MDN:
Array.find Use find if you want to get the
matched object from array else returns Quoting MDN:
See my jsfiddle link There is a polyfill for IE provided by mozilla answered Mar 5, 2016 at 1:39
TeaCoderTeaCoder 1,89812 silver badges13 bronze badges 5 Here's the way I'd do it
answered Nov 29, 2018 at 14:34
Mirza LekaMirza Leka 5716 silver badges9 bronze badges 0 Unless you want to restructure it like this:
to which you can do You will have to loop answered Nov 21, 2011 at 19:33
Keith.AbramoKeith.Abramo 6,9212 gold badges29 silver badges44 bronze badges 2 As per ECMAScript 6 specification, you can use
answered Jan 28, 2017 at 23:54
Lucas BentoLucas Bento 1,2014 gold badges17 silver badges17 bronze badges 2 May
be too late, but javascript array has two methods I think
Some validates that any of the objects in the array satisfies the given condition.
Every validates that all the objects in the array satisfies the given condition. answered Oct 25, 2019 at 8:12
1 As the OP has asked the question if the key exists or not. A more elegant solution that will return boolean using ES6 reduce function can be
Note: The initial parameter of reduce is a Hope it helps for better and cleaner code implementation answered Aug 21, 2018 at 20:19
Jay ChakraJay Chakra 1,4431 gold badge13 silver badges29 bronze badges 5 You cannot without looking into the object really. You probably should change your structure a little, like
Then you can just use it like a lookup-hash.
answered Nov 21, 2011 at 19:34
jAndyjAndy 225k55 gold badges301 silver badges355 bronze badges Testing for array elements:JS Offers array functions which allow you to achieve this relatively easily. They are the following:
The specifics are best explained via an example: Example:
Browser support:These 2 function are
answered Jun 15, 2019 at 9:49
Try this code. If the item or element is present then the output will show you that element. If it is not present then the output will be 'undefined'. answered Nov 10, 2020 at 20:09
2021 Solution*Lodash
ExplanationThis will iterate through the Code snippet
answered Aug 26, 2020 at 23:01
JBallinJBallin 7,0632 gold badges40 silver badges46 bronze badges 0 Simplest method so far:
Zach Jensz 2,8394 gold badges12 silver badges24 bronze badges answered May 13 at 18:56
nhCodernhCoder 3533 silver badges9 bronze badges 0 You have to loop, there is no way around it.
Of course you could use a library like linq.js to make this more pleasing:
(see jsFiddle for a demo) I doubt that linq.js will be faster than a straight-forward loop, but it certainly is more flexible when things get a little more complicated. answered Nov 21, 2011 at 19:35
TomalakTomalak 325k66 gold badges513 silver badges617 bronze badges Correct me if i'm wrong.. i could have used
Nowadays i'm used to it ,because of it simplicity and self explanatory word. Thank you. answered May 22, 2017 at 14:53
Nerdy SidNerdy Sid 3125 silver badges14 bronze badges 1 My approach to solving this problem is to use ES6 and creating a function that does the check for us. The benefit of this function is that it can be reusable through out your project to check any array of objects given the ENOUGH TALK, LET'S SEE THE CODE Array
Function
Call/Usage
answered Jan 31, 2019 at 10:11
rotimi-bestrotimi-best 1,71216 silver badges26 bronze badges 1 Functions Better have something like this:
And use it like this with given OP example:
answered Mar 20, 2021 at 10:50
yurinyurin 4,2451 gold badge36 silver badges31 bronze badges 7 if you're using jquery you can take advantage of grep to create array with all matching objects:
and then use the results array:
answered Oct 12, 2013 at 18:54
EitanEitan 3582 silver badges7 bronze badges You can use lodash. If lodash library is too heavy for your application consider chunking out unnecessary function not used.
This is just one way to do this. Another one can be:
The above example can also be rewritten without using any libraries like:
Hope my answer helps. answered Jan 17, 2018 at 10:01
Abhay ShiroAbhay Shiro 2,9902 gold badges14 silver badges24 bronze badges Many answers here are good and pretty easy. But if your array of object is having a fixed set of value then you can use below trick: Map all the name in a object.
Now this dirtyObj you can use again and again without any loop.
answered Aug 6, 2018 at 7:23
jesusvermajesusverma 1,4931 gold badge14 silver badges17 bronze badges To compare one object to another, I combine a for in loop (used to loop through objects) and some(). You do not have to worry about an array going out of bounds etc, so that saves some code. Documentation on .some can be found here
An alternative way I compare one object to another is to use a nested for loop with Object.keys().length to get the amount of objects in the array. Code below:
To answer your exact question, if are just searching for a value in an object, you can use a single for in loop.
answered Dec 3, 2018 at 23:49
SpangleSpangle 7075 silver badges14 bronze badges Alternatively you can do:
answered Mar 1, 2018 at 20:22
BrunoWestBrunoWest 711 silver badge8 bronze badges 1
Result: without2([{id:1},{id:1},{id:2}],{id:2}) answered Apr 19, 2018 at 10:53
1 You can try this its work for me.
answered May 14, 2019 at 14:14
jenishjenish 1218 bronze badges 2
answered Nov 20, 2019 at 12:41
user1665355user1665355 3,1728 gold badges42 silver badges80 bronze badges 4 I would rather go with regex. If your code is as follows,
I would recommend
answered Mar 5, 2016 at 2:00
3 Not the answer you're looking for? Browse other questions tagged javascript arrays or ask your own question.How do you check if an object is present in an array or not?isArray() method is used to check if an object is an array. The Array. isArray() method returns true if an object is an array, otherwise returns false . Note: For an array, the typeof operator returns an object.
How do you check if an array of objects contain a key?You can iterate through the array, check and see if any of the objects has the key that you are looking for, and return true if it does. If you don't find the key, then the for loop will complete and it will return false.
How do you check if an array of objects contains a value in TypeScript?Use the includes() method to check if an array contains a value in TypeScript, e.g. if (arr. includes('two')) {} . The includes method will return true if the value is contained in the array and false otherwise. Copied!
How do you check if an element is in an object JavaScript?JavaScript provides you with three common ways to check if a property exists in an object: Use the hasOwnProperty() method. Use the in operator. Compare property with undefined .
|