Get key/id from array of objects

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Get key/id from array of objects



In JavaScript I've got the following code to find a specific object in an array:


records.find(function (obj) return obj.time === tmp_date; )



Is it possible to get the key/id of the object from the array records?


records





Post your object here
– Mayank Pandeyz
Aug 9 at 11:31





You could probably use findIndex instead of find, so that you know the index in the records array of the match you got.
– trincot
Aug 9 at 11:32



findIndex


find


records





every object has a keys() method for this. You can use Object.keys(nameOfYourObject). developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
– DEVCNN
Aug 9 at 11:33





.find() returns the found object. So it should be as simple as: const record = records.find( ... ); const id = record.id; Or do you mean the index? Then use .findIndex()
– Shilly
Aug 9 at 11:35



.find()


const record = records.find( ... ); const id = record.id;


.findIndex()





.findIndex() simply returns Array.prototype.findIndex callback must be a function, if I do records.findIndex(function (obj) return obj.time === tmp_date; )
– Alfred Balle
Aug 10 at 9:08


.findIndex()


Array.prototype.findIndex callback must be a function


records.findIndex(function (obj) return obj.time === tmp_date; )




2 Answers
2



Assuming records looks like this (since you didn't provide it) :


records


let records = [

id : 1,
time : 10
,

id : 2,
time : 20

]



then you can simply get the index of the matched object this way :




let records = [

id : 1,
time : 10
,

id : 2,
time : 20

],
tmp_date = 20,
index;

for(let i in records)
if(records[i].time===tmp_date)
index = i;
break;



console.log(`Found time $tmp_date at index $index.`)





The objects do not have id, it's the index I'm looking for. I simply want to know of the found object is the last in the array.
– Alfred Balle
Aug 10 at 9:08


id


index





OK, I get it now. Answer updated.
– Jeremy Thille
Aug 10 at 9:54




find() method on array will return the first matching object based on the condition. You can then fetch the id of that object like we access other properties.


find()


id




var records = [time:10, id:1, time:20, id:2, time:30, id:3];

var id = records.find(function (obj) return obj.time === 20; ).id;

console.log(id);





find() is not supported by IE, developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… . Use a polyfill
– Nirbhay Jha
Aug 9 at 11:42





Yes I am aware about IE support. find() was already there in the question. I have explained how it works and one can fetch the required property from there.
– Rohit Agrawal
Aug 9 at 11:44


find()






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard