2

I have an array like this:

employees = [
        {
        "id": 1,
        "shift_id": 1,
        "days": {
            "2012-03-01": 1,
            "2012-03-02": 1,
            "2012-03-03": 1,
            "2012-03-04": 0,
            "2012-03-05": 0,
            "2012-03-06": 0
        }},
    {
        "id": 2,
        "shift_id": 1,
        "days": {
            "2012-03-01": 0,
            "2012-03-02": 1,
            "2012-03-03": 1,
            "2012-03-04": 1,
            "2012-03-05": 1,
            "2012-03-06": 0
        }},
    {
        "id": 3,
        "shift_id": 2,
        "days": {
            "2012-03-01": 0,
            "2012-03-02": 0,
            "2012-03-03": 1,
            "2012-03-04": 1,
            "2012-03-05": 1,
            "2012-03-06": 1
        }}

    ];

is there a way to access an element in this array using the id value? maybe something in jquery? like $(employees('id = 1');

3
  • 1
    could you not just use ID's as numerical indexes for the array? Commented Oct 15, 2012 at 8:08
  • 1
    That would be easiest, but I have given a detailed answer of how you could do it with some sexy prototypical inheritance. Commented Oct 15, 2012 at 8:23
  • possible duplicate of javascript find value from array based on associative array key value Commented Oct 19, 2014 at 19:32

9 Answers 9

8

Just loop through your array and check for the id:

var yourId = 1;
for (var i = 0, len = employees.length; i < len; i++) {
  if (employees[i].id == yourId) {
     // ...
  }
}
Sign up to request clarification or add additional context in comments.

Comments

4

You can use a function like this, which filters the array appropriately:

var getEmployee = function (id) {
  return employees.filter(function(i) { return i.id == id; });
};

1 Comment

n.b. Array.prototype.filter doesn't have full browser support, but you can always roll your own
4

You can use .grep() method documented here:

var employee = $.grep(employees, function(e) { return e.id == 1 })[0];

Comments

3

Well, there's a jQuery way of doing it:

var findElementById = function(elements, id) {
  return $.grep(elements, function(e) { return e.id === id; })[0];
}

Still I wonder why don't you just index the source array by id instead.

Comments

2

Maybe you are looking for something like the below:

$.grep(employees, function(n){return n.id==1});

Comments

2

Or this:

$.each(employee, function(){
    if(this["id"] == 2){
        console.log(this);
    }
});

1 Comment

But that isn't the OP wants. They need to access the whole object.
1

As far as I am aware, in order to achieve that you would have to loop through them

 Array.prototype.getObjectById = function(x){
      var catcher = false, i = 0;
      while(!catcher){
        catcher = this[i].id == x ? this[i] : false;
        i++;
      }
      return catcher;
    }

This function should help. It will extend the array object so you can use it as myArray.getObjectbyId(id);

By design, this will return the first object that meets the criteria. You could extend it like so:

Array.prototype.getObjectsById = function(x){
  var catcher = [], i = 0;
  for(var i = 0; i < this.length; i++){
    if(this[i].id == value){
      catcher.push(this[i]);
    }
    i++;
  }
  return catcher.length == 1 ? catcher[0] : catcher;
}

This will return an array of objects if more than one object matches the criteria.

 Array.prototype.getObjectsByAttribute = function(x, criteria){
      if(!criteria){criteria = 'id';}
      var catcher = [], i = 0;
      for(var i = 0; i < this.length; i++){
        if(this[i].criteria == value){
         catcher.push(this[i]);
        }
        i++;
      }
      return catcher.length == 1 ? catcher[0] : catcher;
    }

This extends it further to look for any criteria.

5 Comments

please take a look at this: fiddle.jshell.net/AladdinMhaimeed/45cne/14 it gives an error: "Uncaught SyntaxError: Unexpected token catch"
I changed the name of the variable catch to catch1, and now no error :)
yeah, it's a reserved word. I've edited my answer to change it.
I'm sure you've heard this a million times by now, but extending native objects is generally a no-no due to performance issues and bugs it can easily introduce.
It can introduce bugs if poorly used, or used in an environment where multiple developers are working, or likely to work. If the code is well documented and sensible naming conventions are used, the risk is minimal. I am not convinced there would ever be performance issues of real concern. It's a valid tool and should be part of your toolkit for appropriate use. This is an appropriate use for me.
1

I know this question is old, but for future reference if anyone else stumbles upon this question ...

Instead of trying to over-engineer a function to parse/examine your JSON, consider changing the structure of your data to suit its purpose.

Consider the example in the question:

 data =  [ {
    "id": 1,
    "shift_id": 1,
    "days": {
        "2012-03-01": 1,
        "2012-03-02": 1,
        "2012-03-03": 1,
        "2012-03-04": 0,
        "2012-03-05": 0,
        "2012-03-06": 0
    }}, { .. }, {...} ]

Structuring the data in this way only gives you sequential access to the objects with no way to lookup an object by a particular index. Array indices are generally meaningless in this context.

data[0] => { id : 1, .. }
data[1] => { id : 2, .. }

What happens if the id is non-sequential or alphanumeric?

Using an array wont help you search any faster, you'll still have to loop...

Instead consider using a hash table/object:

{
'id1' =>  { id : 1 , data : { ... } }, 
'id99' => { id : 99, data : { ... } },
'id2' =>  { id : 2 , data : { ... } },
}

You can use a string value for the key and get direct access to the data by doing something like:

 data['id2'] => { id : 2, ... }

Which will give you direct access to the data you want to find (by id). By simply re-organizing the structure of the data we were able to go from a O(n) search to an O(1) search.

Keep in mind that this method may work better for some solutions than others, and there are a number of other considerations to make.

This is just one approach to how you might solve a problem when you want to lookup data by a unique property.

1 Comment

reading this before i decided on a data structure helped me a lot, thanks.. Always worth a little bit of planning before diving in :-)
0

The accepted answer is great - modified a bit for an AngularJS app:

  $rootScope.getObjectsByAttribute = function(inarry,infldnm,infldval){
    // This will iterate through a fetchAll sql result set and return rows where fldnm==fldval
    // If it finds 1 row it returns a single object, if more than that it returns an array of objects
    // Usage: result = $rootScope.getObjectsByAttribute(myarray,'myfldnm',myfldval);
    if(!infldnm){infldnm = 'id';}
    var catcher = [], i = 0;
    for(i = 0; i < inarry.length; i++){
      if(inarry[i][infldnm] == infldval){
        catcher.push(inarry[i]);
      }
    }
    return catcher.length == 1 ? catcher[0] : catcher;
  }

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.