使用不同的对象数组更新对象数组

时间:2016-06-30 22:35:09

标签: javascript arrays loops object underscore.js

我有一个非常棘手的操作。所以就是这样。

我有一个名为' data1'

的对象数组
[Object, Object, Object]
       0:Object
               id="00456145"     //check this 
               name: "Rick"
               upper:"0.67"
               lower:"-0.34"
      1:Object
               id="00379321"     
               name:"Anjie"
               upper:"0.46"
               lower:"-0.56"
      2:Object
               id="00323113"      
               name:"dillan"
               upper:"0.23"
               lower:"-0.11"

我只对这些对象数组的id,upper和lower值感兴趣。

以下是名为' data2'

的第二个对象数组
[Object, Object]
     0:Object
             id="0045614"   //present here if we remove last element of '00456145'
             cast="Rick"
             Contact: "Yes"
             upper:"0.11"    //need to be updated to '0.67'
             lower:"-0.11"   //need to be updated to '-0.34'
     1:Object
             id="0032311"     //present here if we remove last element of '00323113'
             cast:"dillan"
             Contact:"Maybe"
             upper:"0.11"
             lower:"-0.11"

所以,这就是我必须要做的。我将首先检查数据1'。 id存在于' data1'检查。对于例如对象0具有id =" 00456145"

我删除了它中的最后一个数字。所以它变成id =" 0045614"。 然后我比较对象' data2'中是否存在此ID。

如果存在,则该对象0的上限和下限值出现在< data1'被传递给对象" data2' id存在的地方。在这种情况下,对象0的数据2'有id =' 0045614'。

因此,上限和下限值将分别更新为0.67和-0.34。

与其他阵列类似。因此,最终输出应该类似于< data2'

[Object, Object]
     0:Object
             id="0045614"   
             cast="Rick"
             Contact: "Yes"
             upper:"0.67"    //updated
             lower:"-0.34"   //updated
     1:Object
             id="0032311"  
             cast:"dillan"
             Contact:"Maybe"
             upper:"0.23"      //updated
             lower:"-0.11"     //updated

6 个答案:

答案 0 :(得分:2)

在这种情况下我喜欢array#some,这样你就可以离开循环,所以没有不必要的迭代:

var data1 = [{ id: "00456145", name: "Rick", upper: "0.67", lower: "-0.34" }, { id: "00379321", name: "Anjie", upper: "0.46", lower: "-0.56" }, { id: "00323113", name: "dillan", upper: "0.23", lower: "-0.11" }];
var data2 = [{ id: "0045614", cast: "Rick", Contact: "Yes", upper: "0.11", lower: "-0.11" }, { id: "0032311", cast: "dillan", Contact: "Maybe", upper: "0.11", lower: "-0.11" }];

data2 = data2.map(function(item) {
  data1.some(function(a) {
    if (item.id == a.id.slice(0, -1)) {
      item.upper = a.upper;
      item.lower = a.lower;
      return true;
    }
  });

  return item;
});

console.log(data2);

答案 1 :(得分:2)

我建议使用对象作为哈希表或地图。然后只需要两个循环,一个用于获取引用,一个用于分配。

大O: O(n + m)

虽然我实际上不知道哪一个(Map vs Object)更适合你,但你可能会得到自己的照片:

提案Object

var data1 = [{ id: "00456145", name: "Rick", upper: "0.67", lower: "-0.34", }, { id: "00379321", name: "Anjie", upper: "0.46", lower: "-0.56", }, { id: "00323113", name: "dillan", upper: "0.23", lower: "-0.11" }],
    data2 = [{ id: "0045614", cast: "Rick", Contact: "Yes", upper: "0.11", lower: "-0.11", }, { id: "0032311", cast: "dillan", Contact: "Maybe", upper: "0.11", lower: "-0.11" }],
    hash = Object.create(null);

data1.forEach(function (a) {
    hash[a.id.slice(0, -1)] = a;
});

data2.forEach(function (a) {
    var o = hash[a.id];
    o && Object.keys(o).forEach(function (k) {
        if (k !== 'id' && a[k] !== o[k]) {
            a[k] = o[k];
        }
    });
});

console.log(data2);

提案Map

var data1 = [{ id: "00456145", name: "Rick", upper: "0.67", lower: "-0.34", }, { id: "00379321", name: "Anjie", upper: "0.46", lower: "-0.56", }, { id: "00323113", name: "dillan", upper: "0.23", lower: "-0.11" }],
    data2 = [{ id: "0045614", cast: "Rick", Contact: "Yes", upper: "0.11", lower: "-0.11", }, { id: "0032311", cast: "dillan", Contact: "Maybe", upper: "0.11", lower: "-0.11" }],
    map = new Map;

data1.forEach(function (a) {
    map.set(a.id.slice(0, -1), a);
});

data2.forEach(function (a) {
    var o = map.get(a.id);
    o && Object.keys(o).forEach(function (k) {
        if (k !== 'id' && a[k] !== o[k]) {
            a[k] = o[k];
        }
    });
});

console.log(data2);

答案 2 :(得分:1)

你可以尝试这样的事情:

for (var i = 0; i < data1.length; i++) {
  var item = data1[i];
  var trim = item.id.substr(0, item.id.length - 1);
  for (var j = 0; j < data2.length; j++) {
    var item2 = data2[j];
    if (item2.id === trim) {
      item2.upper = item.upper;
      item2.lower = item.lower;
    }
  }
}

如果您使用的是es6,则可以通过以下方式简化代码:

for (let item of data1) {
  let trim = item.id.substr(0, item.id.length - 1);
  for (let item2 of data2) {
    if (item2.id === trim) {
      item2.upper = item.upper;
      item2.lower = item.lower;
    }
  }
}

答案 3 :(得分:1)

以下是使用data2的解决方案。循环遍历data1个对象,如果id与var data1 = [{ id: "00456145", name: "Rick", upper: "0.67", lower: "-0.34" }, { id: "00379321", name: "Anjie", upper: "0.46", lower: "-0.56" }, { id: "00323113", name: "dillan", upper: "0.23", lower: "-0.11" }]; var data2 = [{ id: "0045610", //present here if we remove last element of '00456145' cast: "Rick", Contact: "Yes", upper: "0.11", //need to be updated to '0.67' lower: "-0.11" //need to be updated to '-0.34' }, { id: "0032311", //present here if we remove last element of '00323113' cast: "dillan", Contact: "Maybe", upper: "0.11", lower: "-0.11" }]; data2.forEach(function(item2){ data1.forEach(function(item1){ if (item1.id.substring(0,7) === item2.id){ // can be `item1.id.indexOf(item2.id) == 0 item2.upper = item1.upper; item2.lower = item1.lower; } }); }); console.log(data2); 中的任何对象匹配,请更新匹配的对象。

{{1}}

答案 4 :(得分:1)

您可以使用类似

的内容
  data1.forEach(function(obj) {
      var search = obj.id.slice(0, -1);
      data2.forEach(function(d) {
          if (d.id === search) {
              d.upper = obj.upper;
              d.lower = obj.lower;
          };
      });
  });

这样做。

答案 5 :(得分:1)

也许这就是你想要的。

var data1 = [{
  id: "00456145",
  name: "Rick",
  upper: "0.67",
  lower: "-0.34"
}, {
  id: "00379321",
  name: "Anjie",
  upper: "0.46",
  lower: "-0.56"
}, {
  id: "00323113",
  name: "dillan",
  upper: "0.23",
  lower: "-0.11"
}];

var data2 = [{
  id:"0045614",
  cast:"Rick",
  Contact: "Yes",
  upper:"0.11",
  lower:"-0.11",
}, {
  id:"0032311",
  cast:"dillan",
  Contact:"Maybe",
  upper:"0.11",
  lower:"-0.11",
}];


data2.forEach(function(data2Value) {
  data1.forEach(function(data1Value) {
    if(data1Value.id.substr(0, data1Value.id.length - 1) === data2Value.id) {
      data2Value.upper = data1Value.upper;
      data2Value.lower = data1Value.lower;
    }
  });
});

console.log(data2);