Get index in object javascript
For example, I have: Show
Then, I want to sort/reverse this object by
And now I want to know the index of the object with property How do I solve the problem?
asked Aug 24, 2011 at 14:13
5 Since the sort part is already answered. I'm just going to propose another elegant way to get the indexOf of a property in your array Your example is:
You can do:
And here it is with ES6 and arrow syntax, which is even simpler:
xxx 1,1331 gold badge11 silver badges22 bronze badges answered Apr 4, 2014 at 13:54
German AttanasioGerman Attanasio 20.4k7 gold badges47 silver badges63 bronze badges 11 If you're fine with using ES6, arrays now have the findIndex function. Which means you can do something like this:
answered Sep 16, 2016 at 10:19
silverlight513silverlight513 4,8123 gold badges25 silver badges36 bronze badges 5 As the other answers suggest, looping through the array is probably the best way. But I would put it in its own function, and make it a little more abstract:
With this, not only can you find which one contains 'John', but you can find which contains the token '312312':
The function returns -1 when not found, so it follows the same construct as Array.prototype.indexOf().
answered Aug 24, 2011 at 15:45
Chris PickettChris Pickett 2,8121 gold badge13 silver badges7 bronze badges 3 If you're having issues with Internet Explorer, you could use the map() function which is supported from 9.0 onward:
answered Jan 3, 2019 at 1:33
Alain T.Alain T. 35.4k4 gold badges31 silver badges48 bronze badges 1
Which is a simplified version of:
From mozilla.org:
answered Mar 19, 2018 at 19:29
edankedank 5896 silver badges16 bronze badges 4 Only way known to me is to loop through all array:
Or case insensitive:
On result variable index contain index of object or -1 if not found.
answered Aug 24, 2011 at 14:23
Andrew D.Andrew D. 7,9803 gold badges20 silver badges23 bronze badges A prototypical way
answered Jul 24, 2013 at 11:22
1 you can use filter method
answered Nov 22, 2021 at 15:32
Sakhri HoussemSakhri Houssem 8652 gold badges14 silver badges30 bronze badges Just go through your array and find the position:
answered Aug 24, 2011 at 14:24
Sascha GalleySascha Galley 15.2k5 gold badges36 silver badges51 bronze badges 4
answered Sep 4, 2017 at 16:54
answered Jan 26, 2018 at 16:08
2 You can use Array.sort using a custom function as a parameter to define your sorting mechanism. In your example, it would give:
The sort function must return either -1 if a should come before b, 1 if a should come after b and 0 if both are equal. It's up to you to define the right logic in your sorting function to sort the array. Missed the last part of your question where you want to know the index. You would have to loop through the array to find that as others have said.
answered Aug 24, 2011 at 14:24 This might be useful:
I copied this from Working with objects.
answered Feb 4, 2012 at 8:46
AshishAshish 1,17111 silver badges12 bronze badges Use a small workaround: Create a new array with names as indexes. After that all searches will use indexes. So, only one loop. After that you don't need to loop through all elements!
http://jsbin.com/xibala/1/edit Live example.
answered Jan 24, 2015 at 9:01 1 I extended Chris Pickett's answer, because in my case I needed to search deeper than one attribute level:
You can pass 'attr1.attr2' into the function.
answered Nov 30, 2015 at 17:38
Ed SheeEd Shee 9311 gold badge7 silver badges22 bronze badges Use this:
It is assuming you are using Lodash or Underscore.js.
answered Jan 18, 2017 at 11:46
ElloneEllone 3,50211 gold badges37 silver badges67 bronze badges
If you have one Object with multiple objects inside, if you want know if some object are include on Master object, just use
answered Jul 18, 2017 at 14:40
2 If you want to get the value of the property token then you can also try this:
where _.findKey is a Lodash function.
answered Nov 14, 2019 at 12:00
Akash SinghAkash Singh 5374 silver badges7 bronze badges You can use findIndex in Lodash library. Example:
answered Aug 29, 2021 at 12:22
Iman MarashiIman Marashi 5,34736 silver badges50 bronze badges Alternatively to German Attanasio Ruiz's answer, you can eliminate the second loop by using Array.reduce() instead of Array.map();
answered Nov 4, 2014 at 20:45
hypno7oadhypno7oad 1,4311 gold badge19 silver badges28 bronze badges Maybe the Object.keys, Object.entries, and Object.values methods might help. answered Apr 5 at 1:44
milahmilah 11 bronze badge 1 Using Underscore.js:
answered Mar 10, 2020 at 6:04
M Faisal HameedM Faisal Hameed 6531 gold badge6 silver badges22 bronze badges Can we find index of object in JavaScript?JavaScript findIndex() Method: This method returns the index of the first element in an array that satisfies the condition. If this method finds an array element for which the function returns a true value, this method returns the index of that array element and stops, Otherwise, it returns -1.
How do you find the index of an object in an object?To find the index of an object in an array, by a specific property: Use the map() method to iterate over the array, returning only the value of the relevant property. Call the indexOf() method on the returned from map array. The indexOf method returns the index of the first occurrence of a value in an array.
How do you get a specific value from an object in JavaScript?How to get Keys, Values, and Entries in JavaScript Object?. Object.keys(obj) – returns all the keys of object as array.. Object.values(obj) – returns all the values of the object as array.. Object.entries(obj) – returns an array of [key, value]. How do you find the index of an element?To find the index of an element in a list, you use the index() function. It returns 3 as expected. However, if you attempt to find an element that doesn't exist in the list using the index() function, you'll get an error. To fix this issue, you need to use the in operator.
|