JavaScript - 在对象数组

时间:2016-03-17 00:30:17

标签: javascript

我正在尝试在对象数组中搜索对象。

注意,vals和recs对象将是DYNAMIC。

var vals = {ID: "4", LOC: "LA", SEQ: "1"};

var recs = [
  {ID:"4", LOC:"LA", SEQ:"1"},
  {ID:"4", LOC:"NY", SEQ:"1"},
  {ID:"4", LOC:"CHI",SEQ:"1"}

];

现在我需要检查val中的所有key:value对是否已存在于recs中。在这种情况下,recs [0]是val的精确匹配。

继承我的尝试:

var vals =  {ID: "4", LOC: "LA", SEQ: "1"};


var recs = [
  {ID:"4", LOC:"LA", SEQ:"1"},
  {ID:"3", LOC:"NY", SEQ:"2"},
  {ID:"2", LOC:"CHI",SEQ:"3"}

];


for(var i = 0; i<recs.length; i++){  
    if(recs[i]["ID"] == vals["ID"] && recs[i]["LOC"] == vals["LOC"] && recs[i]["SEQ"] == vals["SEQ"]){
      console.log(true);
    }
    else{
      console.log(false);
    }  
}

上述工作只是因为我已经从vals对象硬编码了密钥。实际上,VALS对象(和recs)将是DYNAMIC,具有X个键:值对。

那么如何修改动态vals对象的for循环呢?

谢谢!

6 个答案:

答案 0 :(得分:1)

你需要把它分成两个循环,一个用于数组的每个对象,一个用于对象的每个键:

for(var i = 0; i<recs.length; i++){  
  var found = false
  for(var key in recs[i]) {
    if(recs[i].hasOwnProperty(key)){
      if(recs[i][key] != vals[key]){
        found = true
      }
    }
  console.log(found)
}

如果对象没有该密钥,hasOwnProperty调用将确保它不会中断。

答案 1 :(得分:1)

for(var i = 0; i<recs.length; i++) {  
    for (var prop in object) {
       if (recs[i][prop] != vals[prop]) {
           console.log(false);
           return;
       }

    }
    //check from both sides
    for (var prop in vals) {
       if (recs[i][prop] != vals[prop]) {
           console.log(false);
           return;
       }

    }
    console.log(true);


}

答案 2 :(得分:1)

你可以迭代键;类似的东西:

var vals = {  ID: "4",  LOC: "LA",  SEQ: "1", REGION: "USA" };


var recs = [{    ID: 4,    LOC: "LA",    SEQ: "1",    REGION: "USA"  },
            {    ID: 3,    LOC: "NY",    SEQ: "2",    REGION: "USA"  },
            {    ID: 2,    LOC: "CHI",    SEQ: "3",    REGION: "USA" }

];

var isSame = true;

for (var i = 0; i < recs.length; i++) {
  console.log( i + '----------------' );
  var isSame = true;
  
  // get the keys of the record
  var keys = Object.keys(recs[i]);

  for (var j = 0; j < keys.length; j++) {
    
    var key = keys[j];
    var record = recs[i] 
    
    console.log( key + ": " + record[key] + '=' + vals[key] );   
    
    if (record[key] != vals[key] ) {                                
        isSame = false;// not equal
        break;
    }
  }

  console.log('isSame: ' + isSame );
  console.log('------------------' );
}

答案 3 :(得分:1)

试试这个:

for (var i = 0; i < recs.length; i++) {  
  var found = true;
  for (var p in vals) {
    if (vals.hasOwnProperty(p)) {
      if (recs[i][p] !== vals[p]) {
        found = false;
        break;
      }
    }
  }
  console.log(found);
}

答案 4 :(得分:0)

你可以试试这个:

function myFind(recs, vals) {
    return recs.some(function(obj) {
        for (var x in obj)
            if (x in vals && obj[x] != vals[x])
                return false;
        return true;
    });
}

var recs = [
    {ID:4, LOC:"LA", SEQ:"1", USA:"USA"},
    {ID:3, LOC:"NY", SEQ:"2", USA:"USA"},
    {ID:2, LOC:"CHI",SEQ:"3", USA:"USA"}
];

var vals =  {ID: "4", LOC: "LA", SEQ: "1"};

if (myFind(recs, vals)) {
    alert('found');
} else {
    alert('not found');
}

希望它有所帮助。

答案 5 :(得分:0)

您可以使用underscorejs isEqual来解决此类问题