如何在Javascript数组中合并/推送重复值[关闭]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Javascript数组中合并/推送重复值[关闭]相关的知识,希望对你有一定的参考价值。
我有一个像这样的数组对象:
[
{"item_id":1,"name":"DOTA 2 Backpack","image":"XXX","qty":1,"original_price":1450000,"total_price":1450000},
{"item_id":2,"name":"Mobile Legend Backpack","image":"XXX","qty":1,"original_price":1350000,"total_price":1350000}
]
当我添加具有相同值的数组时,将会是这样的:
[
{"item_id":1,"name":"DOTA 2 Backpack","image":"XXX","qty":1,"original_price":1450000,"total_price":1450000},
{"item_id":2,"name":"Mobile Legend Backpack","image":"XXX","qty":1,"original_price":1350000,"total_price":1350000},
{"item_id":2,"name":"Mobile Legend Backpack","image":"XXX","qty":1,"original_price":1350000,"total_price":1350000}
]
我的目标是希望相同的值将合并如下:
[
{"item_id":1,"name":"DOTA 2 Backpack","image":"XXX","qty":1,"original_price":1450000,"total_price":1450000},
{"item_id":2,"name":"Mobile Legend Backpack","image":"XXX","qty":2,"original_price":1350000,"total_price":2700000}
]
我很困惑并坚持下去。请有人帮助我。
答案
if (items.indexOf(toAdd) != -1)
items.push(toAdd)
或者你可以使用jQuery grep,它返回一个带有ask值的数组列表
if (($.grep(items, function(e){ return e.id == toAdd.id; })) == 0)
items.push(toAdd)
另一答案
如果你想将新的object
推送到array
(如果它不存在)并更新它(如果它存在):
- 你需要先使用Array#some() method检查它的存在。
- 然后使用
qty
索引更新其total_price
和found
。 - 或者,如果它不存在,只需按下即可。
这是你的代码应该如何:
var found = -1;
if (arr.some(function(el, i) {
if (el.name === obj.name)
found = i;
return el.name === obj.name;
})) {
arr[found]["qty"] = arr[found]["qty"] + obj["qty"];
arr[found]["total_price"] = arr[found]["total_price"] + obj["total_price"];
} else {
arr.push(obj);
}
演示:
var arr = [{
"item_id": 'X',
"name": "DOTA 2 Backpack",
"image": "XXX",
"qty": 1,
"original_price": 1450000,
"total_price": 1450000
},
{
"item_id": 'X',
"name": "Mobile Legend Backpack",
"image": "XXX",
"qty": 1,
"original_price": 1350000,
"total_price": 1350000
}
];
var obj = {
"item_id": 'X',
"name": "Mobile Legend Backpack",
"image": "XXX",
"qty": 1,
"original_price": 1350000,
"total_price": 1350000
};
var found = -1;
if (arr.some(function(el, i) {
if (el.name === obj.name)
found = i;
return el.name === obj.name;
})) {
arr[found]["qty"] = arr[found]["qty"] + obj["qty"];
arr[found]["total_price"] = arr[found]["total_price"] + obj["total_price"];
} else {
arr.push(obj);
}
console.log(arr);
另一答案
看看这个。可能会帮助你。
var array1 = [
{"item_id":X,"name":"DOTA 2 Backpack","image":"XXX","qty":1,"original_price":1450000,"total_price":1450000},
{"item_id":X,"name":"Mobile Legend Backpack","image":"XXX","qty":1,"original_price":1350000,"total_price":1350000}
];
var array2 = [
{"item_id":X,"name":"DOTA 2 Backpack","image":"XXX","qty":1,"original_price":1450000,"total_price":1450000},
{"item_id":X,"name":"Mobile Legend Backpack","image":"XXX","qty":1,"original_price":1350000,"total_price":1350000},
{"item_id":X,"name":"Mobile Legend Backpack","image":"XXX","qty":1,"original_price":1350000,"total_price":1350000}
]
var props = ['name', 'item_id'];
var result = array1.filter(function(o1){
return !array2.some(function(o2){
return o1.item_id=== o2.item_id;
});
}).map(function(o){
return props.reduce(function(newo, name){
newo[name] = o[name];
return newo;
}, {});
});
输出:
[
{"item_id":X,"name":"DOTA 2 Backpack","image":"XXX","qty":1,"original_price":1450000,"total_price":1450000},
{"item_id":X,"name":"Mobile Legend Backpack","image":"XXX","qty":1,"original_price":1350000,"total_price":1350000}
]
希望能帮助到你..!
另一答案
你可以使用Array.prototype.reduce() ..在代码示例中,我正在与元素的item_id
进行比较。
码:
const array = [{"item_id": 1,"name":"DOTA 2 Backpack","image":"XXX","qty":1,"original_price":1450000,"total_price":1450000}, {"item_id":2,"name":"Mobile Legend Backpack","image":"XXX","qty":1,"original_price":1350000,"total_price":1350000}];
const newVal = {"item_id": 2,"name":"Mobile Legend Backpack","image":"XXX","qty":1,"original_price":1350000,"total_price":1350000};
const addElement = (arr, val) => {
return arr.reduce((a, c, i) => {
if (c['item_id'] === val['item_id']) {
a.found = true;
c.qty += val.qty;
}
a.result.push(c);
return !a.found && i + 1 === arr.length
? [].concat(arr.result, val)
: a;
}, {result: [], found: false});
};
console.log(addElement(array, newVal).result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
以上是关于如何在Javascript数组中合并/推送重复值[关闭]的主要内容,如果未能解决你的问题,请参考以下文章