Damien
Damien

Reputation: 1600

How to get keys and count of each key from an array of objects

I am trying to get the expected results below, but I am struggling. What is the best way to do it?

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);
})()

Expected Results:

nameL = ['Dave', 'Cae', 'Manuel'];
nameCount = [4, 2, 1];

Upvotes: 0

Views: 67

Answers (5)

stasovlas
stasovlas

Reputation: 7406

another way:

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

Upvotes: 1

connexo
connexo

Reputation: 56744

This does it without any external libraries:

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

Upvotes: 0

Ele
Ele

Reputation: 33726

Use the function forEach along with functions Object.keys and Object.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; }

Upvotes: 0

user3483203
user3483203

Reputation: 51165

How does this work for you? Array.prototype.reduce() just keeps calling this callback on the array while passing in the previous 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'}];

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

console.log(x)

Upvotes: 2

Nenad Vracar
Nenad Vracar

Reputation: 122027

With Lodash you can use countBy method to get one object and then you can use keys and values methods on that object.

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>

Upvotes: 2

Related Questions