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

时间:2018-03-01 18:03:05

标签: javascript arrays function lodash

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

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];

5 个答案:

答案 0 :(得分: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 :(得分:2)

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

&#13;
&#13;
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)
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

另一种方式:

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

答案 3 :(得分: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; }

答案 4 :(得分: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));