Javascript计数重复和唯一,并添加到数组

时间:2021-09-08 20:23:02

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]

[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?

有什么建议么?

5 个解决方案

#1


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:

您可能需要一个对象,而不是数组。所以你正在做的事情已经很好了,但if条件正在搞乱:

$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:

或者以更好的方式,您可以像其他人一样使用.reduce。一个简单的reduce函数将是:

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

#2


2  

For that, you can use .reduce:

为此,您可以使用.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);

#3


2  

You can use reduce and return object

您可以使用reduce和return对象

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

您也可以先创建Object,然后使用forEach添加属性和增量值

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

#4


1  

lodash's countBy function will do the trick:

lodash的countBy函数可以解决这个问题:

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

_.countBy(['a','a','b','c','c'])将评估为:{a:2,b:1,c:2}

It does involve adding lodash as a dependency though.

它确实涉及添加lodash作为依赖。

#5


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

#1


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:

您可能需要一个对象,而不是数组。所以你正在做的事情已经很好了,但if条件正在搞乱:

$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:

或者以更好的方式,您可以像其他人一样使用.reduce。一个简单的reduce函数将是:

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

#2


2  

For that, you can use .reduce:

为此,您可以使用.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);

#3


2  

You can use reduce and return object

您可以使用reduce和return对象

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

您也可以先创建Object,然后使用forEach添加属性和增量值

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

#4


1  

lodash's countBy function will do the trick:

lodash的countBy函数可以解决这个问题:

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

_.countBy(['a','a','b','c','c'])将评估为:{a:2,b:1,c:2}

It does involve adding lodash as a dependency though.

它确实涉及添加lodash作为依赖。

#5


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