Wellington Zanelli
Wellington Zanelli

Reputation: 1964

Comparing arrays with JavaScript or jQuery

I have two arrays, one is a simple array like this:

["Adrian", "Jhon"]

And the another array is an array of objects which I converted from a jSon object. The original jSon was this:

[
    {
        "Nome": "Jhon",
        "Departamento": "Test"
    },
    {
        "Nome": "Adrian",
        "Departamento": "Test"
    },
    {
        "Nome": "Jessy",
        "Departamento": "Test"
    }
]

Now I need to compare the first array, with second one. If the Nome attribute match with my first array, I will return the entire object to another array of objects.

How can I do it with jQuery or pure JavaScript keeping the order of the first array?

EDIT - For stop taking downvotes

I already tried this:

jQuery.each(array, function (x) {
     novoRepo.push(jQuery.grep(repositorio, function (a) {
          return a.Nome == array[x];
     }));
});

But I get an exception saying that a.Nome is undefined.

Upvotes: 1

Views: 152

Answers (3)

mplungjan
mplungjan

Reputation: 177786

Without a filter you can do this:

Live demo

var found = [];
$.each(["Jhon","Adrian"],function(i, name) {
  $.each(otherObject,function(j,obj) {
    if (obj.Nome==name) found.push(obj); // you could leave if only one of each
  });
});

Upvotes: 4

Bergi
Bergi

Reputation: 664356

Use the Array filter method:

var search = ["Jhon","Adrian"],
    data = [
        {"Nome": "Jhon", "Departamento": "Test"},
        {"Nome": "Adrian", "Departamento": "Test"},
        {"Nome": "Jessy", "Departamento": "Test"}
    ];
var result = data.filter(function(obj) {
    return search.indexOf(obj.Nome) >= 0;
});

For those browsers not supporting filter or indexOf (notably IE<9) you can either shim them or use the jQuery equivalents $.grep and $.inArray (see @NULL's answer below for explicit code).


To preserve the order of the search array and not of the data one, you can use map when for every search name there is exactly one result in the data:

result = search.map(function(name) {
    for (var i=0; i<data.length; i++)
        if (data[i].Nome == name)
            return data[i];
});

If there can be none or multiple results for each name, you better used concatMap:

result = Array.prototype.concat.apply(null, search.map(function(name) {
    return data.filter(function(obj) {
         return obj.Nome == name;
    });
});

or with $.map which does automatically unwrap arrays:

result = $.map(search, function(name) {
    return $.grep(data, function(obj) {
         return obj.Nome == name;
    });
});

Upvotes: 8

Andreas Louv
Andreas Louv

Reputation: 47099

To extend @Bergi's answer

Using $.grep and $.inArray would look like this:

var search = ["Jhon","Adrian"],
    data = [
        {"Nome": "Jhon", "Departamento": "Test"},
        {"Nome": "Adrian", "Departamento": "Test"},
        {"Nome": "Jessy", "Departamento": "Test"}
    ];
var result = $.grep(data, function(obj) {
    return $.inArray(obj.Nome, search) >= 0;
});

Upvotes: 2

Related Questions