且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在其中一个属性与另一个数组中的项目不匹配的javascript对象数组中查找项目

更新时间:2023-11-29 23:51:28

您可以尝试以下方法:

var results = [];

objArray.filter(function(item){
    if(simpleArray.indexOf(item.id)>-1)
        results.push(item);
})

请尝试以下代码段:

function Customer(id, name){
  this.id=id;
  this.name=name;
};

this.Customer.prototype.toString = function(){
  return "[id: "+this.id+" name: "+this.name+"]";
}

var objArray = [ new Customer(1,'fred'), new Customer(2,'john'), new Customer(3,'jane'), new Customer(4,'pete')];

var simpleArray = [1,3];

var results = [];

objArray.filter(function(item){
   
    if(simpleArray.indexOf(item.id)>-1)
        results.push(item);
});

document.write(results)