How do you check if an element is in an object javascript?
If Show
If you want
Naetmul 13.6k6 gold badges56 silver badges80 bronze badges answered Dec 14, 2011 at 20:40
ChuckChuck 231k29 gold badges296 silver badges387 bronze badges 25 UPDATE: This answer is incomplete and gives misleading results. For example,
Original answer: Try using EDIT: This answer gives an idea of how to examine variable's properties, but it is not a bulletproof recipe (after all there's no recipe at all!) for checking whether it's an object, far from it. Since people tend to look for something to copy from here without doing any research, I'd highly recommend that they turn to the other, most upvoted (and correct!) answer.
rap-2-h 27.6k32 gold badges160 silver badges249 bronze badges answered Dec 14, 2011 at 20:39
29 Let's define "object" in Javascript. According to the MDN docs, every value is either an object or a primitive:
What's a primitive?
What's an object (i.e. not a primitive)?
How to check whether a value is an object
So I use:
@Daan's answer also seems to work:
because, according to the MDN docs:
A third way that seems
to work (not sure if it's 100%) is to use
answered Mar 18, 2014 at 14:44
Matt FenwickMatt Fenwick 47.3k21 gold badges125 silver badges190 bronze badges 11 underscore.js provides the following method to find out if something is really an object:
UPDATE Because of a previous bug in V8 and minor micro speed optimization, the method looks as follows since underscore.js 1.7.0 (August 2014):
answered Feb 5, 2013 at 11:50
DaanDaan 7,4055 gold badges41 silver badges52 bronze badges 12
For more information on this and why it is a good alternative to typeof, check out this article.
Ionică Bizău 104k82 gold badges274 silver badges456 bronze badges answered Nov 13, 2012 at 6:20
ChristopheChristophe 26.3k25 gold badges94 silver badges137 bronze badges 10 For simply checking against Object or Array without additional function call (speed). As also posted here. isArray()
isLiteralObject() - Note: use for Object literals only, as it returns false for custom objects, like new Date or new YourCustomObject.
answered May 17, 2013 at 11:39
zupazupa 12.3k5 gold badges39 silver badges39 bronze badges 18 With function Array.isArray:
Without function Array.isArray:Just surprised how many upvotes for wrong answers 😮
As for me, it's clear and simple, and just works! Here my tests:
ONE MORE TIME: not all answers pass this tests !!! 🙈 In case you need to verify that object is instance of particular class you have to check constructor with your particular class, like:
simple test:
As result, you will have strict and robust code! In case you won't create functions like
it won't work correctly for all test cases mentioned earlier, but it's good enough for all objects (plain or constructed).
There is already created package on npm v1 based on this implementation! And it works for all earlier described test cases! 🙂 answered Oct 10, 2017 at 9:21
cn007bcn007b 16.2k6 gold badges57 silver badges70 bronze badges 7 Oh My God! I think this could be more shorter than ever, let see this: Short and Final code
ExplainedReturn Typestypeof JavaScript objects (including
Checking on Their constructorsChecking on their
Introducing Function.name
answered Jul 21, 2018 at 16:27
3 OK, let's give you this concept first before answering your question, in JavaScript Functions are Object, also null, Object, Arrays and even Date, so as you see there is not a simple way like typeof obj === 'object', so everything mentioned above will return true, but there are ways to check it with writing a function or using JavaScript frameworks, OK: Now, imagine you have this object that's a real object (not null or function or array):
Pure JavaScript:
or
or
or
You can simply use one of these functions as above in your code by calling them and it will return true if it's an object:
If you are using a JavaScript framework, they usually have prepared these kind of functions for you, these are few of them: jQuery:
Angular:
Underscore and Lodash:
answered Feb 15, 2017 at 13:39
AlirezaAlireza 95.7k26 gold badges263 silver badges167 bronze badges 2 It depends on what you mean with "is an object". If you want everything that is not a primitive, i.e. things that you can set new properties on, this should do the trick:
It excludes the primitives (plain numbers/ If you want to know whether something is a "plain" object, i.e. it was created as a literal
Edit 2018: Because answered Jan 18, 2016 at 21:12
last-childlast-child 4,0211 gold badge20 silver badges18 bronze badges 2 My God, too much confusion in other answers. Short Answer
To test this simply run the following statements in chrome console. Case 1.
Case 2.
Case 3.
Explanation Okay.Let's break it down
Drum rolls please! By this you may have already learnt how to check for Array in Javascript. answered Sep 24, 2018 at 11:45
HalfWebDevHalfWebDev 6,47512 gold badges60 silver badges96 bronze badges 2 Try this
answered May 9, 2012 at 11:43
TalhaTalha 18.4k8 gold badges47 silver badges65 bronze badges 4 The most reasonable way to check the type of a value seems the
Therefore, instead of Comprehensive but not exhaustive list of proper ways to test if a value belongs to the Object type.
Note: I intentionally skipped some approaches like answered Aug 28, 2016 at 0:48
OriolOriol 257k57 gold badges409 silver badges493 bronze badges 5 Little late... for "plain objects" (i mean, like {'x': 5, 'y': 7}) i have this little snippet:
It generates the next output:
It always works for me. If will return "true" only if the type of "o" is "object", but no null, or array, or function. :) answered Oct 25, 2015 at 16:26
Emilio GrisolíaEmilio Grisolía 1,1731 gold badge9 silver badges14 bronze badges 1
Ready to use functions for checking
Explanation
Tests
answered Nov 17, 2016 at 23:43
Inanc GumusInanc Gumus 22.7k9 gold badges83 silver badges97 bronze badges 1 Here's an answer with optional chaining, and perhaps the smallest
answered May 8, 2020 at 17:42
Jayant BhawalJayant Bhawal 1,7342 gold badges25 silver badges31 bronze badges 7
If you would like to check if the
Or as a single-expression arrow function (ES6+)
answered Apr 16, 2018 at 14:22
sasisasi 3242 silver badges9 bronze badges 2
I was asked to provide
more details. Most clean and understandable way of checking if our variable is an object is Unfortunately either Array and null also have a type So the solution is:
answered Jun 10, 2016 at 7:41
KaniaKania 2,2322 gold badges29 silver badges34 bronze badges 2 lodash has isPlainObject, which might be what many who come to this page are looking for. It returns false when give a function or array. answered Oct 20, 2014 at 19:33
PatPat 16.1k14 gold badges93 silver badges112 bronze badges 1 PerformanceToday 2020.09.26 I perform tests on MacOs HighSierra 10.13.6 on Chrome v85, Safari v13.1.2 and Firefox v80 for chosen solutions. Results
DetailsI perform 3 tests cases for solutions A B C D E F G H I J K L M N O P Q R S T U V
Below snippet presents differences between solutions. Solutions A-G give proper answers for chosen cases described by Matt Fenwick And here are example results for chrome answered Sep 25, 2020 at 22:41
Kamil KiełczewskiKamil Kiełczewski 75.9k26 gold badges335 silver badges313 bronze badges The Ramda functional library has a wonderful function for detecting JavaScript types. Paraphrasing the full function:
I had to laugh when I realized how simple and beautiful the solution was. Example usage from Ramda documentation:
answered Feb 22, 2018 at 21:34
DaveGauerDaveGauer 1,23514 silver badges24 bronze badges After reading and trying out a lot of implementations, I've noticed that very few people try to check for values like Instead of checking the After all, the
answered Sep 10, 2019 at 2:11
Gust van de WalGust van de Wal 5,1031 gold badge21 silver badges45 bronze badges When everything else fails, I use this:
answered Sep 18, 2014 at 18:42
MichalMichal 1311 silver badge1 bronze badge 5 This will work. It is a function that returns true, false, or possibly null.
answered Apr 5, 2017 at 6:41
pizzarobpizzarob 11.3k6 gold badges45 silver badges66 bronze badges 1 Since there seems a lot of confusion about how to handle this problem correctly, I'll leave my 2 cents (this answer is spec compliant and produces correct results under all circumstances): Testing for primitives:
An object is not a primitive:
Or alternatively:
Testing for any Array:
Testing for object excluding:
answered Nov 12, 2018 at 14:06
c7x43tc7x43t 2642 silver badges5 bronze badges For the purpose of my code I found out this decision which corresponds with some of the answers above: ES6 variant:
ES5 variant:
You can use it very simply:
and so on.. answered Jun 23, 2020 at 10:29
ChristiyanChristiyan 3374 silver badges4 bronze badges 2
answered Dec 16, 2014 at 0:49
IraIra 7368 silver badges7 bronze badges It is an old question but thought to leave this here. Most people are checking if the variable is
Most of the time what we want is to know if we have a resource object from an API or our database call returned from the ORM. We can then test if is not an
answered Nov 27, 2019 at 18:28
redeemefyredeemefy 4,0836 gold badges33 silver badges50 bronze badges 2
Based from lodash answered Apr 12, 2020 at 21:48
insigninsign 4,75736 silver badges32 bronze badges 1 How do you check if a property is present in an object JavaScript?The hasOwnProperty() method returns true if the specified property is a direct property of the object — even if the value is null or undefined . The method returns false if the property is inherited, or has not been declared at all.
How do I check if an object contains something?Using includes() Method: If array contains an object/element can be determined by using includes() method. This method returns true if the array contains the object/element else return false. Example: html.
How do you check if a key exists in an object?There are mainly two methods to check the existence of a key in JavaScript Object. The first one is using “in operator” and the second one is using “hasOwnProperty() method”. Method 1: Using 'in' operator: The in operator returns a boolean value if the specified property is in the object.
How can you tell if an element is an array or an object?Answer: Use the Array. isArray() Method
isArray() method to check whether an object (or a variable) is an array or not. This method returns true if the value is an array; otherwise returns false .
|