简体   繁体   English

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

[英]How do I compare and update this array based on another array in JavaScript?

I am working on this challenge—Inventory update , on Free CodeCamp.com The challenge states: 我正在研究此挑战 -Free CodeCamp.com上的库存更新 ,挑战指出:

Compare and update the inventory stored in a 2D array against a second 2D array of a fresh delivery. 与新鲜交货的第二个2D阵列比较并更新2D阵列中存储的库存。 Update the current existing inventory item quantities (in arr1). 更新当前现有库存物料数量(以arr1为单位)。 If an item cannot be found, add the new item and quantity into the inventory array. 如果找不到物料,则将新物料和数量添加到库存阵列中。 The returned inventory array should be in alphabetical order by item. 返回的库存数组应按字母顺序排列。

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;

}

When I try: 当我尝试:

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

When I try: 当我尝试:

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

I get: 我得到: 在此处输入图片说明

When I should get: 什么时候应该得到:

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

I believe this is where my problem is... 我相信这就是我的问题所在。

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


    }

cause it's sort of working. 因为它有点工作。 But can anyone help with the alphabetical sorting part too? 但是,有人可以在字母排序部分上提供帮助吗? Thanks in advance! 提前致谢!

Try this 尝试这个

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;

}

for sorting 用于分类

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

You could do this as well, I know its been answered but just wanted to give it a go. 您也可以这样做,我知道它已经回答了,但只想尝试一下。

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

You could use the built in array functions to make things a bit easier to understand. 您可以使用内置的数组函数使事情更容易理解。 In my example I'm sure you could combine all the functions instead of altering the oldValues array. 在我的示例中,我确定您可以组合所有功能,而不用更改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 http://plnkr.co/edit/VNksR4eoDLEqU7TSoTj1?p=preview

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据另一个数组在Javascript中重新排序此数组? - How do I reorder this array in Javascript based on another array? 如何根据另一个数组更新 Javascript 数组中的值? - How to update values in Javascript array based on another array? 如何根据另一个数组更新数组中的对象? - How do you update an object in an array based on another array? 如何根据另一个数组的索引在一个数组中找到一个数组? - How do I find an array in an array based on index of another array? 如何根据匹配索引上的另一个数组更新数组? - How can I update an array based on another array on matching index? 如何比较单个数字与另一个数组中的不同数字? Java脚本 - How do i compare a single number with different numbers in another array? Javascript 如何根据 JavaScript 中的另一个数组对 object 数据的数组进行分组、重组和计数 - How do I group, restructure and count array of object data based on another array in JavaScript 如何使用Javascript基于另一个数组的相应元素的属性对数组进行排序? - How do I sort an array based on the attribute of corresponding elements of another array using Javascript? 如何根据另一个数组的顺序对对象数组进行排序? - How do I sort an array of objects based on the ordering of another array? 如何基于另一个对象数组对一个对象数组进行排序 - How do I sort an array of objects based on another array of objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM