如何从对象数组中获取每个键的键和计数

问题描述 投票:0回答:5

我想在下面得到预期的结果,但我正在努力。最好的方法是什么?

const data = [{
    name: 'Dave',
    country: 'England',
    color: 'Brown'
  },
  {
    name: 'Dave',
    country: 'England',
    color: 'white'
  },
  {
    name: 'Cae',
    country: 'USA',
    color: 'white'
  },
  {
    name: 'Dave',
    country: 'England',
    color: 'Red'
  },
  {
    name: 'Cae',
    country: 'USA',
    color: 'Yellow'
  },
  {
    name: 'Dave',
    country: 'England',
    color: 'white'
  },
  {
    name: 'Manuel',
    country: 'USA',
    color: 'Red'
  },
  {
    name: 'Dave',
    country: 'England',
    color: 'white'
  }
];


// Tentative:

(function getDataForName() {
  count = 0;
  nameL = [];
  nameCount = [];
  for (let i = 0; i < data.length; i++) {
    if (nameL.indexOf(data[i].name) === -1) {
      nameL.push(data[i].name);
      count++;
    }
    nameCount.push(count);
  }
  console.log(nameL);
  console.log(nameCount);
})()

预期成绩:

nameL = ['Dave', 'Cae', 'Manuel'];
nameCount = [4, 2, 1];
javascript arrays function lodash
5个回答
2
投票

使用Lodash,您可以使用countBy方法获取一个对象,然后您可以在该对象上使用keysvalues方法。

const data = [{name: 'Dave', country: 'England', color: 'Brown'},{name: 'Dave', country: 'England', color: 'white'},{name: 'Cae', country: 'USA', color: 'white'},{name: 'Dave', country: 'England', color: 'Red'},{name: 'Cae', country: 'USA', color: 'Yellow'},{name: 'Dave', country: 'England', color: 'white'},{name: 'Manuel', country: 'USA', color: 'Red'},{name: 'Dave', country: 'England', color: 'white'}]

const result = _.countBy(data, 'name')
const names = _.keys(result);
const count = _.values(result);

console.log(names);
console.log(count)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>

2
投票

这对你有什么用? Array.prototype.reduce()只是在传入以前的值时继续在数组上调用此回调。

const data = [{name: 'Dave', country: 'England', color: 'Brown'},{name: 'Dave', country: 'England', color: 'white'},{name: 'Cae', country: 'USA', color: 'white'},{name: 'Dave', country: 'England', color: 'Red'},{name: 'Cae', country: 'USA', color: 'Yellow'},{name: 'Dave', country: 'England', color: 'white'},{name: 'Manuel', country: 'USA', color: 'Red'},{name: 'Dave', country: 'England', color: 'white'}];

x = data.reduce(function(sums,entry){
   sums[entry.name] = (sums[entry.name] || 0) + 1;
   return sums;
},{});

console.log(x)

1
投票

其他方式:

const [nameL, nameCount] = _.chain(data)
    .groupBy('name')
    .mapValues(_.size)
    .toPairs()
    .thru(_.spread(_.zip))
    .value();

0
投票

使用函数forEach以及函数Object.keysObject.values

const data = [    {name: 'Dave', country: 'England', color: 'Brown'},    {name: 'Dave', country: 'England', color: 'white'},    {name: 'Cae', country: 'USA', color: 'white'},    {name: 'Dave', country: 'England', color: 'Red'},    {name: 'Cae', country: 'USA', color: 'Yellow'},    {name: 'Dave', country: 'England', color: 'white'},    {name: 'Manuel', country: 'USA', color: 'Red'},    {name: 'Dave', country: 'England', color: 'white'}    ],
      a = {};

data.forEach((c) => a[c.name] = (a[c.name] || 0) + 1);

console.log(JSON.stringify(Object.keys(a)));
console.log(JSON.stringify(Object.values(a)));
.as-console-wrapper { max-height: 100% !important; top: 0; }

0
投票

这样做没有任何外部库:

const data=[{name:'Dave',country:'England',color:'Brown'},{name:'Dave',country:'England',color:'white'},{name:'Cae',country:'USA',color:'white'},{name:'Dave',country:'England',color:'Red'},{name:'Cae',country:'USA',color:'Yellow'},{name:'Dave',country:'England',color:'white'},{name:'Manuel',country:'USA',color:'Red'},{name:'Dave',country:'England',color:'white'}];

const result = data.reduce(function(count, entry){
   count[entry.name] = count[entry.name] ? count[entry.name] + 1 : 1;
   return count;
},{});

console.log(Object.keys(result));
console.log(Object.values(result));
© www.soinside.com 2019 - 2024. All rights reserved.