javascript - Convert Array to new Array (counting occurrences) - Stack Overflow

I have an array of n number of itemsvar arr1 = [2, 0, 0, 1, 1, 2, 0, 0, 0, 2, 1, 1, 0, 1, 0, 1, 0, 0, 1

I have an array of n number of items

var arr1 = [2, 0, 0, 1, 1, 2, 0, 0, 0, 2, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 2, 2, 1, 2, 2, 0, 1, 2, 2, 1, 1, 0, 1, 1, 0, 2, 1, 0, 0, 0, 2, 1, 1, 1, 2, 2, 1, 0, 0, 0, 2, 2, 2, 2, 2, 1, 0, 2, 2, 0, 2, 2, 0, 2, 0, 0, 1, 2, 1, 0, 2, 1, 0, 1, 2, 0, 2, 0, 0, 0, 1, 2, 1, 0, 2, 0, 0, 0, 1, 2, 1, 1, 1, 1]

as you see the array only has i different values (v) (0,1,2),i = 3 in that case

What I would like is ending up with an array like this one.

var arr2 = [23, 45, 64]

the length of the arr2 array should corresponds to i and the values sould be the occurences of each value(v)

I am doing all kinds of loops and conditionals, but looking for a straight solution. my part so far /

jQuery and/or underscore may be involved.

I have an array of n number of items

var arr1 = [2, 0, 0, 1, 1, 2, 0, 0, 0, 2, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 2, 2, 1, 2, 2, 0, 1, 2, 2, 1, 1, 0, 1, 1, 0, 2, 1, 0, 0, 0, 2, 1, 1, 1, 2, 2, 1, 0, 0, 0, 2, 2, 2, 2, 2, 1, 0, 2, 2, 0, 2, 2, 0, 2, 0, 0, 1, 2, 1, 0, 2, 1, 0, 1, 2, 0, 2, 0, 0, 0, 1, 2, 1, 0, 2, 0, 0, 0, 1, 2, 1, 1, 1, 1]

as you see the array only has i different values (v) (0,1,2),i = 3 in that case

What I would like is ending up with an array like this one.

var arr2 = [23, 45, 64]

the length of the arr2 array should corresponds to i and the values sould be the occurences of each value(v)

I am doing all kinds of loops and conditionals, but looking for a straight solution. my part so far http://jsfiddle/fiddlebjoern/aSsjy/2/

jQuery and/or underscore may be involved.

Share Improve this question edited Mar 4, 2013 at 13:39 j0k 22.8k28 gold badges81 silver badges90 bronze badges asked Mar 8, 2012 at 11:27 BjörnBjörn 13.2k12 gold badges55 silver badges70 bronze badges 1
  • see also stackoverflow./questions/5667888/… – georg Commented Mar 8, 2012 at 12:19
Add a ment  | 

4 Answers 4

Reset to default 8

Easy peasy! The values of your input bee the keys of your output; you accumulate on those keys as you encounter the values:

var arr1 = [0,0,0,1,2,3,4,3,2,3,4,3,4,3,5];
var arr2 = [];

for (var i = 0; i < arr1.length; i++) {
   var n = arr1[i];
   if (arr2[n] != undefined)
       arr2[n]++;
   else
       arr2[n] = 1;
}

console.log(arr2);  // Output: [3, 1, 2, 5, 3, 1]

Live demo.

Sounds like a job for "reduce"

arr2 = arr1.reduce(function(a, v) {
    a[v] = (a[v] || 0) + 1;
    return a;
}, [])

Also, your arr2 is actually associative, so it's better to use an object instead:

map = arr1.reduce(function(a, v) {
    a[v] = (a[v] || 0) + 1;
    return a;
}, {})

reduce is Javascript 1.8, for older browsers you need an emulation or use a library like underscore.js.

Example with underscore.js:

arr2 = _(arr1).chain().groupBy(_.identity).map(_.size).value()

This is perhaps not so "easy" as other answers, but at least you can learn something from this.

For the sake of being plete, here's the correct way to use a plain loop for the same task:

counter = [] // or {}
for (var i = 0; i < arr.length; i++)
     counter[arr[i]] = (counter[arr[i]] || 0) + 1;

Based on the other answer, here's one that works:

var arr1 = [1,2,3,4,3,2,3,4,3,4,3,5];
var arr2 = [];
var n, m;

for (var i=0, iLen=arr1.length; i < iLen; i++) {
  n = arr1[i];
  m = arr2[n];
  arr2[n] = m? ++m : 1;
}

alert(arr2)

You can use array_count_values:

<?php
     $array = array(1, "hello", 1, "world", "hello");
     print_r(array_count_values($array));
?>

The output is:

Array
(
    [1] => 2
    [hello] => 2
    [world] => 1
)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744199635a4562819.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信