2

Considering I have an array of objects, and all objects represent something out of a database, thus they have an unique identifier.

Now I also have the ID and the correct array. How do I search each object in that array where the parameter 'id' equals my ID. (The point is, I don't know the internal identifier for that object. All I have is an ID and I need the entire object for description, last_user, created etc..)

Object
created: "2011-06-08 15:47:11"
description: "Something new.."
id: "1"
last_user: "1"

P.s. I have jQuery embedded, so if there's no default way, a jQuery function would suffice.

2 Answers 2

3

$.grep() should do it. In the following example arr is your array of objects. It will find the element that has an id of 1.

var obj = jQuery.grep(arr, function(el, i){
  return el.id == 1;
})[0];
Sign up to request clarification or add additional context in comments.

2 Comments

dont you mean return el.id == 1; ?
Nice, thank you. Not sure what the .get(0) is for. It doesn't work for me. I now have this: var refId = this.id; var refObj = $.grep(markers[theme], function(el, i){ return el.id == refId; })[0];
0

You could loop through your array of objects, and check for each one if yourObject.id is equal to the id you are looking for. Then you'll be able to get the other fields, such as yourObject.created

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.