我有一个像这样的JSON对象:
long_array =
[
{"location":"Kirrawee","identity_long":"student"},
{"location":"Kirrawee","identity_long":"visitor"},
{"location":"Kirrawee","identity_long":"visitor"},
{"location":"Kirrawee","identity_long":"worker"},
{"location":"Sutherland","identity_long":"student"},
{"location":"Sutherland","identity_long":"resident"},
{"location":"Sutherland","identity_long":"worker"},
{"location":"Sutherland","identity_long":"resident"},
{"location":"Miranda","identity_long":"resident"},
{"location":"Miranda","identity_long":"worker"},
{"location":"Miranda","identity_long":"student"},
{"location":"Miranda","identity_long":""},
{"location":"Miranda","identity_long":"worker"},
{"location":"Miranda","identity_long":"resident"}
];我想要达到的目标如下:
grouped_and_counted_location_and_identity =
[
{"location":"Kirrawee","identity":"student","count":1},
{"location":"Kirrawee","identity":"visitor","count":2},
{"location":"Kirrawee","identity":"worker","count":1},
{"location":"Sutherland","identity":"student","count":1},
{"location":"Sutherland","identity":"resident","count":2},
{"location":"Sutherland","identity":"worker","count":1},
{"location":"Miranda","identity":"resident","count":2},
{"location":"Miranda","identity":"worker","count":2},
{"location":"Miranda","identity":"student","count":1}
];我发现这在R语言中非常容易实现,我会这样做:
long_array %>%
group_by(location, identity_long) %>%
summarise(n = n())甚至只是
long_array %>%
count(location, identity_long)但是,如何在javascript中实现这一点呢?
我只想将JSON对象按两个属性分组,并计数相同的出现次数。
发布于 2020-09-27 05:08:10
您可以使用库,如存档,并使用它的组逐功能来完成这一简单的方式,位耗时的方式将是实现您自己的分组的功能。
let long_array = [{
"location": "Kirrawee",
"identity_long": "student"
},
{
"location": "Kirrawee",
"identity_long": "visitor"
},
{
"location": "Kirrawee",
"identity_long": "visitor"
},
{
"location": "Kirrawee",
"identity_long": "worker"
},
{
"location": "Sutherland",
"identity_long": "student"
},
{
"location": "Sutherland",
"identity_long": "resident"
},
{
"location": "Sutherland",
"identity_long": "worker"
},
{
"location": "Sutherland",
"identity_long": "resident"
},
{
"location": "Miranda",
"identity_long": "resident"
},
{
"location": "Miranda",
"identity_long": "worker"
},
{
"location": "Miranda",
"identity_long": "student"
},
{
"location": "Miranda",
"identity_long": ""
},
{
"location": "Miranda",
"identity_long": "worker"
},
{
"location": "Miranda",
"identity_long": "resident"
}
];
function addItemCounts(items, groupByKeys) {
var groups = _.groupBy(long_array, obj => {
return groupByKeys.map(key => obj[key]).join('-');
});
return _.map(groups, g => ({
...g[0],
count: g.length
}));
}
console.log(addItemCounts(long_array, ['identity_long', 'location']));<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
发布于 2020-09-27 04:56:33
使用long_array中的所有值创建一组数组和compair的唯一值,并计算相同的值并将其保存在新的数组中
const long_array = [{
"location": "Kirrawee",
"identity_long": "student"
},
{
"location": "Kirrawee",
"identity_long": "visitor"
},
{
"location": "Kirrawee",
"identity_long": "visitor"
},
{
"location": "Kirrawee",
"identity_long": "worker"
},
{
"location": "Sutherland",
"identity_long": "student"
},
{
"location": "Sutherland",
"identity_long": "resident"
},
{
"location": "Sutherland",
"identity_long": "worker"
},
{
"location": "Sutherland",
"identity_long": "resident"
},
{
"location": "Miranda",
"identity_long": "resident"
},
{
"location": "Miranda",
"identity_long": "worker"
},
{
"location": "Miranda",
"identity_long": "student"
},
{
"location": "Miranda",
"identity_long": ""
},
{
"location": "Miranda",
"identity_long": "worker"
},
{
"location": "Miranda",
"identity_long": "resident"
}
];
const unique_values = [...new Map(long_array.map(obj => [JSON.stringify(obj), obj])).values()]; // this will remove duplicate values from long_array
const result = unique_values.map((val) => { // iterate in unique_values
let count = 0;
long_array.forEach((item) => {
item.location == val.location && item.identity_long === val.identity_long && count++
}); //iterate in long_array and count same values
return { ...val,
count: count
}
})
console.log(result);
发布于 2020-09-27 05:24:04
这里有一个O(n*logn)时间复杂度和O(n)空间复杂度的解决方案。
let long_array =
[
{ "location": "Kirrawee", "identity_long": "student" },
{ "location": "Kirrawee", "identity_long": "visitor" },
{ "location": "Kirrawee", "identity_long": "visitor" },
{ "location": "Kirrawee", "identity_long": "worker" },
{ "location": "Sutherland", "identity_long": "student" },
{ "location": "Sutherland", "identity_long": "resident" },
{ "location": "Sutherland", "identity_long": "worker" },
{ "location": "Sutherland", "identity_long": "resident" },
{ "location": "Miranda", "identity_long": "resident" },
{ "location": "Miranda", "identity_long": "worker" },
{ "location": "Miranda", "identity_long": "student" },
{ "location": "Miranda", "identity_long": "" },
{ "location": "Miranda", "identity_long": "worker" },
{ "location": "Miranda", "identity_long": "resident" }
];
// create map
let map = new Map()
for (let i = 0; i < long_array.length; i++) {
const s = JSON.stringify(long_array[i])
if (!map.has(s)) {
// if the map does not contain the object already
// i.e. its first occurrence
map.set(s, {
location: long_array[i].location,
identity: long_array[i].identity_long,
count: 1,
})
} else {
// if it no first occurrence
// increase the count straight way
map.get(s).count++
}
}
const result = Array.from(map.values())
console.log(result)
https://stackoverflow.com/questions/64084819
复制相似问题