0

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];
2
  • Is this a learning exercise or just need a snippet that works? chrisz answer works, but might be confusing to a learner Commented Mar 1, 2018 at 18:11
  • Remember to pick your answer carefully so others see your problem was solved. If you have any questions regarding any of the answers given, feel free to ask in the comments of that answer. Commented Mar 1, 2018 at 23:57

5 Answers 5

2

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>

Sign up to request clarification or add additional context in comments.

2 Comments

You have an elegant solution using vanilla ES6?
@connexo that's not vallina JS ES6
2

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)

Comments

1

another way:

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

1 Comment

You can replace the .groupBy() and .mapValues() pair with a single countBy('name') call. Additionally, you don't need to use the explicit chain method, _.chain(data), you can safely use the implicit chain method, _(data), in this case.
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.