使用JavaScript或jQuery比较数组

时间:2021-11-21 12:12:50

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:

另一个数组是我从jSon对象转换的对象数组。最初的jSon是这样的:

[
    {
        "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.

现在我需要比较第一个数组,第二个数组。如果Nome属性与我的第一个数组匹配,我将整个对象返回到另一个对象数组。

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

如何使用jQuery或纯JavaScript保持第一个数组的顺序?

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.

但我得到一个例外,说a.Nome是未定义的。

3 个解决方案

#1


4  

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
  });
});

#2


8  

Use the Array filter method:

使用Array过滤器方法:

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).

对于那些不支持filter或indexOf(特别是IE <9)的浏览器,您可以对它们进行填充或使用jQuery等效的$ .grep和$ .inArray(请参阅下面的@ NULL的答案以获取显式代码)。


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:

为了保留搜索数组的顺序而不是数据的顺序,您可以使用map,对于每个搜索名称,数据中只有一个结果:

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:

如果每个名称都没有或多个结果,最好使用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:

或者使用$ .map自动解包数组:

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

#3


2  

To extend @Bergi's answer

扩展@ Bergi的答案

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

使用$ .grep和$ .inArray看起来像这样:

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;
});

#1


4  

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
  });
});

#2


8  

Use the Array filter method:

使用Array过滤器方法:

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).

对于那些不支持filter或indexOf(特别是IE <9)的浏览器,您可以对它们进行填充或使用jQuery等效的$ .grep和$ .inArray(请参阅下面的@ NULL的答案以获取显式代码)。


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:

为了保留搜索数组的顺序而不是数据的顺序,您可以使用map,对于每个搜索名称,数据中只有一个结果:

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:

如果每个名称都没有或多个结果,最好使用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:

或者使用$ .map自动解包数组:

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

#3


2  

To extend @Bergi's answer

扩展@ Bergi的答案

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

使用$ .grep和$ .inArray看起来像这样:

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;
});