首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Javascript将JSON对象按两个属性分组并计数

Javascript将JSON对象按两个属性分组并计数
EN

Stack Overflow用户
提问于 2020-09-27 04:32:21
回答 7查看 1.4K关注 0票数 1

我有一个像这样的JSON对象:

代码语言:javascript
复制
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"}
];

我想要达到的目标如下:

代码语言:javascript
复制
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语言中非常容易实现,我会这样做:

代码语言:javascript
复制
long_array %>%
    group_by(location, identity_long) %>%
    summarise(n = n())

甚至只是

代码语言:javascript
复制
long_array %>%
    count(location, identity_long)

但是,如何在javascript中实现这一点呢?

我只想将JSON对象按两个属性分组,并计数相同的出现次数。

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2020-09-27 05:08:10

您可以使用库,如存档,并使用它的组逐功能来完成这一简单的方式,位耗时的方式将是实现您自己的分组的功能。

代码语言:javascript
复制
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']));
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>

票数 1
EN

Stack Overflow用户

发布于 2020-09-27 04:56:33

使用long_array中的所有值创建一组数组和compair的唯一值,并计算相同的值并将其保存在新的数组中

代码语言:javascript
复制
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);

票数 3
EN

Stack Overflow用户

发布于 2020-09-27 05:24:04

这里有一个O(n*logn)时间复杂度和O(n)空间复杂度的解决方案。

代码语言:javascript
复制
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)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64084819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档