0

I'm trying to count duplicates in an array of dates and add them to a new array.

But i'm only getting the duplicates and the amount of times they exist in the array.

What I want is:

[a, a, b, c, c] => [a: 2, b: 1, c: 2]

Code:

$scope.result = { };
    for(var i = 0; i < $scope.loginArray.length; ++i) {
        if(! $scope.result[$scope.loginArray[i]]){
             $scope.result[$scope.loginArray[i]] = 0;
        ++ $scope.result[$scope.loginArray[i]];}
    }

Any suggestions?

1
  • you just need to move this line out of if ++ $scope.result[$scope.loginArray[i]]; , otherwise the value not get updated on next iteration Commented Jun 7, 2016 at 9:10

5 Answers 5

2

You might need an object for this, not an array. So what you are doing is already great, but the if condition is messing up:

$scope.result = {};
for (var i = 0; i < $scope.loginArray.length; ++i) {
  if (!$scope.result[$scope.loginArray[i]])
    $scope.result[$scope.loginArray[i]] = 0;
  ++$scope.result[$scope.loginArray[i]];
}

Snippet

var a = ['a', 'a', 'b', 'c', 'c'];
var r = {};
for (var i = 0; i < a.length; ++i) {
  if (!r[a[i]])
    r[a[i]] = 0;
  ++r[a[i]];
}
console.log(r);

Or in better way, you can use .reduce like how others have given. A simple reduce function will be:

var a = ['a', 'a', 'b', 'c', 'c'];
var r = a.reduce(function(c, e) {
  c[e] = (c[e] || 0) + 1;
  return c;
}, {});

console.log(r);

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

Comments

2

For that, you can use .reduce:

var arr = ['a','a', 'b', 'c', 'c'];
var result = arr.reduce(function(p,c){
  if(p[c] === undefined)
    p[c] = 0;
  p[c]++;
  return p;
},{});

console.log(result);

Comments

2

You can use reduce and return object

var ar = ['a', 'a', 'b', 'c', 'c'];
var result = ar.reduce(function(r, e) {
  r[e] = (r[e] || 0) + 1;
  return r;
}, {});

console.log(result)

You can also first create Object and then use forEach add properties and increment values

var ar = ['a', 'a', 'b', 'c', 'c'], result = {}
ar.forEach(e => result[e] = (result[e] || 0)+1);
console.log(result)

Comments

1

lodash's countBy function will do the trick:

_.countBy(['a', 'a', 'b', 'c', 'c']) will evaluate to: {a: 2, b: 1, c: 2}

It does involve adding lodash as a dependency though.

2 Comments

It would be better to use the native JavaScript functionality instead of using a library just for this, agree?
You are 100% correct. I stated that it does involve adding a dependency, which can be a burden. Although lodash is a very small library and could be useful in many more cases.
0

I would do like this

var arr = ["a", "a", "b", "c", "c", "a"],
  red = arr.reduce((p,c) => (p[c]++ || (p[c]=1),p),{});
console.log(red);

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.