[英]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"]])
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.