gpt4 book ai didi

JavaScript 用于删除对象数组中的重复对象并对属性求和

转载 作者:行者123 更新时间:2023-11-28 19:45:56 25 4
gpt4 key购买 nike

我已经尝试了好几天来解决我的问题,但我就是做不到,想象我有以下 JSON 数组(我们将其称为 jsonData:

[
{ "id": 118748, "price":"", "stocklevel": 100, "instock": false, "pname": "Apple TV" },
{ "id": 118805291, "price":"", "stocklevel": 432, "instock": true, "pname": "Hitachi TV"},
{ "id": 118801891, "price":"", "stocklevel": 0, "instock": false, "pname": "Sony TV" },
{ "id": 118748, "price":"", "stocklevel": 2345, "instock": true, "pname": "Apple TV"},
...

现在我的 JSON 数组中可能有超过 100 个项目,我想删除具有重复 id 的项目,但对库存水平求和并保留数组中的顺序,因此一行应该取代最新出现的项目那个id。在上面的 JSON 中,带有“id”的对象的第一个实例:118748 被删除,但它的库存水平值传递/添加了具有相同 id 的对象的下一个实例,因此 JSON 数组如下所示:

[
{ "id": 118805291, "price":"", "stocklevel": 432, "instock": true, "pname": "Hitachi TV"},
{ "id": 118801891, "price":"", "stocklevel": 0, "instock": false, "pname": "Sony TV" },
{ "id": 118748, "price":"", "stocklevel": 2445, "instock": true, "pname": "Apple TV"},
...

我生成了以下代码来删除重复项,但我无法对库存水平总数进行求和,这是我的代码:

function idsAreEqual(obj1, obj2) {
return obj1.id === obj2.id;
}

function arrayContains(arr, val, equals) {
var i = arr.length;
while (i--) {
if (equals(arr[i], val)) {
return true;
}
}
return false;
}

function removeDups(arr, equals) {
var originalArr = arr.slice(0);
var i, k, len, val;
arr.length = 0;

for (i = originalArr.length - 1, len = originalArr.length, k = originalArr.length - 1 ; i > 0; --i) {
val = originalArr[i];

if (!arrayContains(arr, val, equals)) {
arr.push(val);
}
}
}


removeDups(jsonData, idsAreEqual);
jsonData.reverse();

有人可以帮我解决这个问题吗?请注意,我无法使用 Underscore、jQuery 或任何其他库。

提前非常感谢

最佳答案

您可以执行如下操作,我认为这比您的实现简单一些。我还使用 forEachreduce,它们是 ES5,但应该适用于现代浏览器。如果您使用的是较旧的、不兼容 ES5 的浏览器,则可以获取适用于以下任一浏览器的 polyfil:

var data = [
{ "id": 118748, "price":"", "stocklevel": 0, "instock": false, "pname": "Apple TV" },
{ "id": 118805291, "price":"", "stocklevel": 432, "instock": true, "pname": "Hitachi TV"},
{ "id": 118801891, "price":"", "stocklevel": 0, "instock": false, "pname": "Sony TV" },
{ "id": 118748, "price":"", "stocklevel": 2345, "instock": true, "pname": "Apple TV"}
];

function dedup_and_sum(arr, prop) {
var seen = {},
order = [];
arr.forEach(function(o) {
var id = o[prop];
if (id in seen) {
// keep running sum of stocklevel
var stocklevel = seen[id].stocklevel + o.stocklevel
// keep this newest record's values
seen[id] = o;
// upid[118805291], stocklevel=432, instock=truedate stocklevel to our running total
seen[id].stocklevel = stocklevel;
// keep track of ordering, having seen again, push to end
order.push(order.splice(order.indexOf(id), 1));
}
else {
seen[id] = o;
order.push(id);
}
});

return order.map(function(k) { return seen[k]; });
}

// Get unique records, keeping last record of dups
// and summing stocklevel as we go
var unique = dedup_and_sum(data, 'id');
unique.forEach(function(o) {
console.log("id[%d], stocklevel=%d, instock=%s", o.id, o.stocklevel, o.instock);
});
// output =>
// id[118805291], stocklevel=432, instock=true
// id[118801891], stocklevel=0, instock=false
// id[118748], stocklevel=2445, instock=true

编辑已更新以符合我们在评论中讨论的要求。

关于JavaScript 用于删除对象数组中的重复对象并对属性求和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24290196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com