gpt4 book ai didi

javascript - 如何根据属性合并数组中的对象

转载 作者:行者123 更新时间:2023-11-28 00:07:01 27 4
gpt4 key购买 nike

我试图合并数组中的重复对象,同时添加它们的一些属性值并将属性连接到数组中,但我被卡住了。

这是我正在处理的数组类型:

[{
"Favorites": 0,
"Frequency": 1,
"Hashtag": "19h30",
"Replies": 0,
"Retweets": 1,
"tweetId": 615850479952785400
}, {
"Favorites": 0,
"Frequency": 1,
"Hashtag": "80s",
"Replies": 0,
"Retweets": 1,
"tweetId": 617319253738393600
}, {
"Favorites": 0,
"Frequency": 1,
"Hashtag": "80s",
"Replies": 0,
"Retweets": 1,
"tweetId": 616521677275533300
}, {
"Favorites": 1,
"Frequency": 1,
"Hashtag": "AloeBlacc",
"Replies": 0,
"Retweets": 1,
"tweetId": 617309488572420100
}, {
"Favorites": 2,
"Frequency": 1,
"Hashtag": "Alpes",
"Replies": 0,
"Retweets": 1,
"tweetId": 615481266348146700
}]

我希望找到的最终结果:

[{
"Favorites": 0,
"Frequency": 1,
"Hashtag": "19h30",
"Replies": 0,
"Retweets": 1,
"tweetId": 615850479952785400
}{
"Favorites": 0,
"Frequency": 2,
"Hashtag": "80s",
"Replies": 0,
"Retweets": 2,
"tweetId": [617319253738393600 ,
616521677275533300]
}, {
"Favorites": 1,
"Frequency": 0,
"Hashtag": "AloeBlacc",
"Replies": 0,
"Retweets": 1,
"tweetId": 617309488572420100
}, {
"Favorites": 2,
"Frequency": 0,
"Hashtag": "Alpes",
"Replies": 0,
"Retweets": 1,
"tweetId": 615481266348146700
}]

目前,我尝试在循环中循环,但没有成功,我陷入了困境。

var mergedHashtags = [];
var merged={};
for (var i = 0; i < hashtags.length-1; i++) {
var a = hashtags[i];
// start second loop at next element in array
for (var j = i+1; j < hashtags.length; j++) {
var b = hashtags[j];
console.log('a',a,'b',b, 'i',i,'j',j);
if (a.Hashtag===b.Hashtag) {
var tIds = [a.tweetId];
merged.Hashtag = a.Hashtag;
merged.tweetId = tIds.concat(b.tweetId);
merged.Favorites = a.Favorites+b.Favorites;
merged.Retweets = a.Retweets+b.Retweets;
merged.Replies = a.Replies+b.Replies;
merged.Frequency = a.Frequency+b.Frequency;
mergedHashtags.push(merged);
console.log('same', merged);
continue;
}else{
mergedHashtags.push(a);
i=j-1;
console.log('diff',a);
break;
}
}
}

我将非常感谢您的帮助!

提前致谢问:

最佳答案

您可以创建临时存储object并按其 Hashtag 存储项目,因此您不必再次循环查找是否存在具有相同 Hashtag 的项目.

var merge = function(list) {
var result = [];
var store = {};
var addables = ['Favorites', 'Retweets', 'Replies', 'Frequency'];

list.forEach(function(item) {
// Check exist by hashtag.
if (typeof store[item.Hashtag] === 'undefined') {
// clone item, not alter origin list.
store[item.Hashtag] = JSON.parse(JSON.stringify(item));
result.push(store[item.Hashtag]);
} else {
var soruce = store[item.Hashtag];
// Check if its already combined or not.
if( Object.prototype.toString.call(soruce.tweetId) === '[object Array]') {
soruce.tweetId.push(item.tweetId);
} else {
soruce.tweetId = [soruce.tweetId, item.tweetId];
}
// Add addable attributes.
addables.forEach(function(key) {
soruce[key] += item[key];
});
}
});

return result;
};

var list = [{
"Favorites": 0,
"Frequency": 1,
"Hashtag": "19h30",
"Replies": 0,
"Retweets": 1,
"tweetId": 615850479952785400
}, {
"Favorites": 0,
"Frequency": 1,
"Hashtag": "80s",
"Replies": 0,
"Retweets": 1,
"tweetId": 617319253738393600
}, {
"Favorites": 0,
"Frequency": 1,
"Hashtag": "80s",
"Replies": 0,
"Retweets": 1,
"tweetId": 616521677275533300
}, {
"Favorites": 1,
"Frequency": 1,
"Hashtag": "AloeBlacc",
"Replies": 0,
"Retweets": 1,
"tweetId": 617309488572420100
}, {
"Favorites": 2,
"Frequency": 1,
"Hashtag": "Alpes",
"Replies": 0,
"Retweets": 1,
"tweetId": 615481266348146700
}];

console.log(merge(list));

关于javascript - 如何根据属性合并数组中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31249518/

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