0

Using jQuery, I would like to use an array of ids to find the objects inside the allObjects array that have the matching Id value.

var arrayOfIds = [1, 4, 5];

var allObjects = [{"Id":"1", "name":"aa"},{"Id":"2", "name":"bb"} ,{"Id":"3", "name":"cc"} ,{"Id":"4", "name":"dd"}, {"Id":"5", "name":"ee"}, {"Id":"6", "name":"ff"}, {"Id":"7", "name":"gg"}, {"Id":"8", "name":"hh"}, {"Id":"9", "name":"ii"}];

The result would equal:

[{"Id":"1", "name":"aa"}, {"Id":"4", "name":"dd"}, {"Id":"5", "name":"ee"}]

So far, I can only use the following to extract an individual object:

var result = $.grep(arrayOfIds, function(e) { return e.Id == 3; });

I feel as though the answer might be achieved by amending the above $.grep query somehow but can't figure it out.

3 Answers 3

3

You don't need jQuery for this. You can use Array.prototype.filter() to filter allObjects and Array.prototype.includes() to check if the objects Id property is in arrayOfIds:

allObjects.filter(x=> arrayOfIds.includes(Number(x.Id)))

See demo on JS Bin.

Sign up to request clarification or add additional context in comments.

2 Comments

According to MDN, includes is not yet supported in IE, if that is a concern.
@Taplar There's a polyfill for it.
0

Best is you transform the array to an object itself:

function atoo(a)
{
  var i, obj;

  obj = {};

  for (i = 0; i < a.length; i++)
  {
    obj[a[i].Id] = a[i];
  }

  return obj;
}

You can now access all items in the array through the object by simply addressing them as an index:

obj["4"]

returns the correct object that is also stored in the array under a[3].

There is no jQuery involved which should be considered a feature because it is a general solution to all kinds of these problems.

Using a filter (as in Array.prototype.filter()) is easier to write but also incurs in performance problems when you access the items very often or the array is very large. The above solution relies on the internal implementation of the object referencing which is as fast as you can wish for.

Comments

0

You can use filter() method like following.

var arrayOfIds = [1, 4, 5];
var allObjects = [{ "Id": "1", "name": "aa" }, { "Id": "2", "name": "bb" }, { "Id": "3", "name": "cc" }, { "Id": "4", "name": "dd" }, { "Id": "5", "name": "ee" }, { "Id": "6", "name": "ff" }, { "Id": "7", "name": "gg" }, { "Id": "8", "name": "hh" }, { "Id": "9", "name": "ii" }];
var result = $(allObjects).filter(function() { return arrayOfIds.indexOf(+this.Id) > -1 }).get();

2 Comments

Don't use + operator to turn a string into a number, it makes code unreadable. Also, why are you using .indexOf() when there is .includes() method?
Better to not use +, but I don't find any problem with indexOf(). @Gothdo

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.