gpt4 book ai didi

javascript - 查找具有多次匹配属性的 JSON 对象

转载 作者:数据小太阳 更新时间:2023-10-29 05:42:37 26 4
gpt4 key购买 nike

我需要在 json 数组中找到具有相同名称属性的每个元素,例如这里的 Alaska 是两次,然后我需要比较两个对象的 lastupdate 并选择具有最新更新时间的那个。采用 stackoverflow 中的答案(抱歉,我丢失了链接)我可以删除具有相同名称属性的对象,但如何保留具有最新更新时间的对象?

[{
"name": "Alaska",
"Republican_fre": 3,
"Democrats_fre": 0,
"winner": "R",
"iso_2": "AK",
"electoral_vote": 3,
"totalComponents": 3,
"date": "29.06.2016",
"lastupdate": "1467233426"
}, {
"name": "Alabama",
"Republican_fre": 3,
"Democrats_fre": 0,
"winner": "R",
"iso_2": "AL",
"electoral_vote": 9,
"totalComponents": 3,
"date": "29.06.2016",
"lastupdate": "1467233426"
}, {
"name": "Arkansas",
"Republican_fre": 2,
"Democrats_fre": 0,
"winner": "R",
"iso_2": "AR",
"electoral_vote": 6,
"totalComponents": 2,
"date": "29.06.2016",
"lastupdate": "1467233426"
},
{
"name": "Alaska",
"Republican_fre": 5,
"Democrats_fre": 0,
"winner": "R",
"iso_2": "AK",
"electoral_vote": 3,
"totalComponents": 5,
"date": "29.06.2016",
"lastupdate": "1467282133"
}]

代码:

function arrUnique(arr) {
var cleaned = [];
data.forEach(function(itm) {
var unique = true;
cleaned.forEach(function(itm2) {
var minValue = Math.min(itm.lastupdate, itm2.lastupdate)
if (_.isEqual(itm.name, itm2.name)){
unique = false;
}
});
if (unique) cleaned.push(itm);
});
return cleaned;
}

var uniqueStandards = arrUnique(data);

jsfiddle:

预期输出预期的输出是它保留具有最新“lastupdate”值的 Alsaka 对象之一。所以它首先检查具有相同名称属性的对象,然后比较上次更新值并保留具有最新值的对象

最佳答案

您可以使用下划线的 sortBy()lastupdate 键对集合中的项目进行排序,reverse()让所有项目按 lastupdate 降序排列,然后使用 uniq()仅保留唯一的 name 项。

var uniqueStandards = _.uniq(_.sortBy(data, 'lastupdate').reverse(), 'name');

var data = [{
"name": "Alaska",
"Republican_fre": 3,
"Democrats_fre": 0,
"winner": "R",
"iso_2": "AK",
"electoral_vote": 3,
"totalComponents": 3,
"date": "29.06.2016",
"lastupdate": "1467233426"
}, {
"name": "Alabama",
"Republican_fre": 3,
"Democrats_fre": 0,
"winner": "R",
"iso_2": "AL",
"electoral_vote": 9,
"totalComponents": 3,
"date": "29.06.2016",
"lastupdate": "1467233426"
}, {
"name": "Arkansas",
"Republican_fre": 2,
"Democrats_fre": 0,
"winner": "R",
"iso_2": "AR",
"electoral_vote": 6,
"totalComponents": 2,
"date": "29.06.2016",
"lastupdate": "1467233426"
}, {
"name": "Alaska",
"Republican_fre": 5,
"Democrats_fre": 0,
"winner": "R",
"iso_2": "AK",
"electoral_vote": 3,
"totalComponents": 5,
"date": "29.06.2016",
"lastupdate": "1467282133"
}];

var uniqueStandards = _.uniq(_.sortBy(data, 'lastupdate').reverse(), 'name');

document.body.innerHTML = '<pre>' + JSON.stringify(uniqueStandards, 0, 4) + '</pre>';
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>


普通的 JS 解决方案是:

var uniqueStandards = data
.slice() // this makes sure that we're not mutating the original array
.sort(function(x, y) { return y.lastupdate - x.lastupdate; }) // sort in descending order
.filter(function(x) { // this ensure items with unique names
return (this[x.name]? false: (this[x.name] = true));
}, {});

var data = [{
"name": "Alaska",
"Republican_fre": 3,
"Democrats_fre": 0,
"winner": "R",
"iso_2": "AK",
"electoral_vote": 3,
"totalComponents": 3,
"date": "29.06.2016",
"lastupdate": "1467233426"
}, {
"name": "Alabama",
"Republican_fre": 3,
"Democrats_fre": 0,
"winner": "R",
"iso_2": "AL",
"electoral_vote": 9,
"totalComponents": 3,
"date": "29.06.2016",
"lastupdate": "1467233426"
}, {
"name": "Arkansas",
"Republican_fre": 2,
"Democrats_fre": 0,
"winner": "R",
"iso_2": "AR",
"electoral_vote": 6,
"totalComponents": 2,
"date": "29.06.2016",
"lastupdate": "1467233426"
}, {
"name": "Alaska",
"Republican_fre": 5,
"Democrats_fre": 0,
"winner": "R",
"iso_2": "AK",
"electoral_vote": 3,
"totalComponents": 5,
"date": "29.06.2016",
"lastupdate": "1467282133"
}];

var uniqueStandards = data
.slice() // this makes sure that we're not mutating the original array
.sort(function(x, y) { return y.lastupdate - x.lastupdate; }) // sort in descending order
.filter(function(x) { // this ensure items with unique names
return (this[x.name]? false: (this[x.name] = true));
}, {});

document.body.innerHTML = '<pre>' + JSON.stringify(uniqueStandards, 0, 4) + '</pre>';


或者,您可以提供 lodash尝试一下:

var uniqueStandards = _(data).orderBy('lastupdate', 'desc').uniqBy('name').value();

上面的代码片段使用了 orderBy()lastupdate 降序排列集合,uniqBy()以确保该集合仅具有唯一名称。

var data = [{
"name": "Alaska",
"Republican_fre": 3,
"Democrats_fre": 0,
"winner": "R",
"iso_2": "AK",
"electoral_vote": 3,
"totalComponents": 3,
"date": "29.06.2016",
"lastupdate": "1467233426"
}, {
"name": "Alabama",
"Republican_fre": 3,
"Democrats_fre": 0,
"winner": "R",
"iso_2": "AL",
"electoral_vote": 9,
"totalComponents": 3,
"date": "29.06.2016",
"lastupdate": "1467233426"
}, {
"name": "Arkansas",
"Republican_fre": 2,
"Democrats_fre": 0,
"winner": "R",
"iso_2": "AR",
"electoral_vote": 6,
"totalComponents": 2,
"date": "29.06.2016",
"lastupdate": "1467233426"
}, {
"name": "Alaska",
"Republican_fre": 5,
"Democrats_fre": 0,
"winner": "R",
"iso_2": "AK",
"electoral_vote": 3,
"totalComponents": 5,
"date": "29.06.2016",
"lastupdate": "1467282133"
}];

var uniqueStandards = _(data).orderBy('lastupdate', 'desc').uniqBy('name').value();

document.body.innerHTML = '<pre>' + JSON.stringify(uniqueStandards, 0, 4) + '</pre>';
<script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>

关于javascript - 查找具有多次匹配属性的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38190244/

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