如何使用 javascript 在下面的数组中添加相同的 id 数量?



>我有两个数组,它们是

product_id = [5,5,10,15,5,15,22]
product_qty = [58,40,120,100,98,100,50]

(我已经从表中存储为序列到数组中,ID和数量订单与我上面提到的相同。

我想用他们的 id 计算同一 id 的总数。结果应该是

result_id = [5,10,15,22] //no duplicates
result_qty = [196,120,200,50] //sum to same id's quantity

如何在JavaScript中解决此问题?

一种可能的解决方案(保留问题中指定的两个数组解决方案,尽管您可能希望查看 Vineswaran 在注释中指定的哈希值)是遍历第一个数组(使用 ids)并推送值(如果索引数组中不存在),或者如果索引数组中存在值,则添加值, 喜欢这个:

var product_id = [5,5,10,15,5,15,22];
var product_qty = [58,40,120,100,98,100,50];
var result_id = [];
var result_qty = [];
// traverse the product ids array
$.each(product_id, function(idx, val) {
// check if that product id had a previous instance
var auxIdx = $.inArray(val, result_id)
if (auxIdx >= 0) {
// if it did, add the quantities
result_qty[auxIdx] += product_qty[idx];
} else {
// if it didn't, push both id and quantity into the result arrays
result_id.push(val);
result_qty.push(product_qty[idx]);
}
});
console.log(result_id);
console.log(result_qty);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

你也可以在这个JSFiddle上看到它:http://jsfiddle.net/pmkLcLdd/

我没有你的样本数据。所以,我在评论中修改了你的代码。

var store_product = {}; 
$(data).each(function(did,value){
var product_place_id = value.product_place_id_primary;
var ids = getProductAndPlaceId(product_place_id);
var item_quantity = store_product[ids[0]];
if (item_quantity) {
store_product[ids[0]] = (item_quantity + value.quantity);
} else {
store_product[ids[0]] = value.quantity;
}
});

store_product哈希将获得您的预期结果。您可以根据需要将其转换为数组或任何内容。

你不需要jquery。

var product_id = [5,5,10,15,5,15,22]
var product_qty = [58,40,120,100,98,100,50]
var result_id = product_id.slice(); //Copy arrays
var result_qty = product_qty.slice()
var i = result_id.length;
while(i > 0){
var id = result_id.shift();              //remove first element
--i;
var sum = result_qty.shift();            //init sum count
var index = result_id.indexOf(id, 0);    //find next match of element
while(index != -1){
result_id.splice(index, 1);            //remove element in index
--i;
sum += result_qty.splice(index, 1)[0]; //index 0, since splice return a list with length 1
index = result_id.indexOf(id,index);
}
result_id.push(id);                      //add to end
result_qty.push(sum);
}
console.log(result_id);
console.log(result_qty);
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

我认为一个对象更适合你想要的东西,这样,你可以轻松地将id与其quantity联系起来,id是关键:

var product_id = [5,5,10,15,5,15,22],
product_qty = [58,40,120,100,98,100,50],
result_qty = {};
product_qty.forEach(function (qty, i) {
var indexVal = result_qty[product_id[i]] || 0;
result_qty[product_id[i]] = indexVal + qty;
});
console.log(result_qty); 
// Logs Object {5: 196, 10: 120, 15: 200, 22: 50}
console.log(result_qty[5]);
// Logs 196
//////////////////////////////////////////////////////////////////
// * However, if you really want both, the ids and quantities in
// * array format, then it's just a matter of running this,
// * after the previous code:
var tmp = [],
result_id = [];
// * (looping through the object)
for(var prop in result_qty) {
if(result_qty.hasOwnProperty(prop)) {
tmp.push(result_qty[prop]);
result_id.push(parseInt(prop, 10));
}
}
result_qty = tmp;
console.log(result_id);
// Logs [196, 120, 200, 50]
console.log(result_qty);
// Logs [5, 10, 15, 22]

无论如何,我已经提供了一种获取数组的方法,因此您有两个选择。

最新更新