如何在Javascript数组中合并/推送重复值[关闭]
Posted
技术标签:
【中文标题】如何在Javascript数组中合并/推送重复值[关闭]【英文标题】:How to merge/push duplicate value in Javascript Array [closed] 【发布时间】:2018-07-01 23:20:11 【问题描述】:我有一个这样的数组对象:
[
"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
]
我很困惑并坚持下去。请有人帮助我。
【问题讨论】:
你在使用 lodash 吗? @Axax 不,顺便说一句,我是 javascript 的新手。 不,Item_id 不同。示例:Dota 2 -> item_id : 1 Mobile Legend -> item_id : 2 不,X 是假的。 你的“合并”代码是什么? 【参考方案1】:您可以使用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;
【讨论】:
非常感谢。解决了! :)【参考方案2】:看看这个。或许能帮到你。
您可以使用Array Iteration methods
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
]
希望对您有所帮助..!
【讨论】:
你读过这个问题吗?这不能回答问题,也不能解决 OP 问题。 非常感谢。解决了! :) @Axaxaxaxax 如果解决了。标记一下。谢谢【参考方案3】:如果您想将新的object
推送到array
,如果它不存在并更新它,如果它存在:
found
索引更新其qty
和total_price
。
或者如果它不存在,只需推送它。
这应该是你的代码:
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);
【讨论】:
非常感谢。解决了! :) @Axaxaxaxax 欢迎您,很高兴它有帮助。如果对您有帮助,您可以接受。【参考方案4】:if (items.indexOf(toAdd) != -1)
items.push(toAdd)
或者你可以使用 jQuery grep,它返回一个包含请求值的数组列表
if (($.grep(items, function(e) return e.id == toAdd.id; )) == 0)
items.push(toAdd)
【讨论】:
你知道我的问题吗?以上是关于如何在Javascript数组中合并/推送重复值[关闭]的主要内容,如果未能解决你的问题,请参考以下文章