如何根据JavaScript中的另一个数组比较和更新此数组?

时间:2016-05-03 23:13:34

标签: javascript arrays

我在Free CodeCamp.com上正在研究这个challenge—Inventory update 挑战表明:

  

比较并更新存储在2D数组中的库存与秒   2D阵列的新鲜交付。更新当前现有库存   项目数量(在arr1中)。如果找不到项目,请添加新项目   物料和数量进入库存数组。退回的库存   数组应按项目的字母顺序排列。

function updateInventory(arr1, arr2) {
    for (var x = 0; x < arr1.length; x++) {

        for (var y = 0; y < arr2.length; y++) {

            if (arr1[x][1] === arr2[y][1]) {
                arr1[x][0] = arr2[y][0]  + arr1[x][0];
            }
        }
    }

 for(var j = 0; j < arr2.length; j++){

     var i = arr2[j][1];

     for(var k = 0; k < arr1.length; k++){
         var idx = arr1.indexOf(i);
          if(idx === -1){
             arr1.push(i);
          }
     }


}
return arr1;

}

当我尝试:

updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], 
[1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], 
[3, "Half-Eaten Apple"], [67, "Bowling Ball"], 
[7, "Toothpaste"]]).length // I get 8 instead of 6

当我尝试:

updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], 
[1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], 
[3, "Half-Eaten Apple"], [67, "Bowling Ball"], 
[7, "Toothpaste"]])

我得到: enter image description here

什么时候得到:

[[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], 
[3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]]

我相信这就是我的问题......

for(var j = 0; j < arr2.length; j++){

         var i = arr2[j][1];

         for(var k = 0; k < arr1.length; k++){
             var idx = arr1.indexOf(i);
              if(idx === -1){
                 arr1.push(i);
              }
         }


    }

因为它有点工作。但是,任何人都可以帮助按字母排序部分吗?提前谢谢!

3 个答案:

答案 0 :(得分:1)

试试这个

function updateInventory(arr1, arr2) {
  for (var x = 0; x < arr1.length; x++) {

    for (var y = 0; y < arr2.length; y++) {

      if (arr1[x][1] === arr2[y][1]) {
        arr1[x][0] = arr2[y][0] + arr1[x][0];
      }
    }
  }

  for (var j = 0; j < arr2.length; j++) {

    var i = arr2[j][1];
    var found = 0;

    for (var k = 0; k < arr1.length; k++) {
      if (arr1[k][1] === arr2[j][1]){
        found = 1;}
    }
    if (found == 0)
      arr1.push(arr2[j]);
  }
  arr1.sort(compareSecondColumn);
  return arr1;

}

用于排序

function compareSecondColumn(a, b) {
    if (a[1] === b[1]) {
        return 0;
    }
    else {
        return (a[1] < b[1]) ? -1 : 1;
    }
}

答案 1 :(得分:1)

你也可以这样做,我知道它已经得到了解答但只是想试一试。

&#13;
&#13;
Array.prototype.concatAll = function() {
  var results = [];
  this.forEach(function(subArray) {
    subArray.forEach(function(item) {
      results.push(item);
    });
  });
  return results;
};

function updateInventory(arr1, arr2) {
  arr1.forEach(function(item) {
    for (var i = 0; i < arr2.length; i++) {
      if (item[1] === arr2[i][1]) {
        item[0] = item[0] + arr2[i][0];
      }
    }
  });

  arr1.forEach(function(item) {
    var tempArray = arr1.concatAll();
    for (var e = 0; e < arr2.length; e++) {
      if (tempArray.indexOf(arr2[e][1]) === -1) {
        arr1.push(arr2[e]);
      }
    }
  });

  arr1 = arr1.sort(function(a, b) {
    return (a[1] > b[1]) ? 1 : ((b[1] > a[1]) ? -1 : 0);
  });
  alert(arr1);

  return arr1;
}

// Example inventory lists
var curInv = [
  [21, "Bowling Ball"],
  [2, "Dirty Sock"],
  [1, "Hair Pin"],
  [5, "Microphone"]
];

var newInv = [
  [2, "Hair Pin"],
  [3, "Half-Eaten Apple"],
  [67, "Bowling Ball"],
  [7, "Toothpaste"]
];

updateInventory(curInv, newInv);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以使用built in array functions让事情更容易理解。在我的示例中,我确定您可以组合所有函数,而不是更改oldValues数组。

var oldValues = [[21, "Bowling Ball"], [2, "Dirty Sock"], 
[1, "Hair Pin"], [5, "Microphone"]];

var newValues = [[2, "Hair Pin"], 
[3, "Half-Eaten Apple"], [67, "Bowling Ball"], 
[7, "Toothpaste"]];


newValues.forEach(function(nArr) {
  var i = 0;
  for(i; i < oldValues.length; i++) {
    if(oldValues[i][1] === nArr[1]) {
      oldValues[i] = nArr;
      return;
    }
  }
  oldValues.push(nArr);
});

oldValues.sort(function (ov, nv) {
  return ov[1] > nv[1];
}).forEach(function (v) {
  console.debug(v);
  $('#test').append('<li>[' + v[0] + ', ' + v[1] + ']</li>');
});

http://plnkr.co/edit/VNksR4eoDLEqU7TSoTj1?p=preview