而不是JavaScript的array_count_values

时间:2011-03-07 07:48:43

标签: php javascript phpjs

我有以下PHP脚本,现在我需要在JavaScript中做同样的事情。 JavaScript中的函数是否与PHP函数类似,我一直在搜索但找不到类似的东西?我想要做的是计算某个单词在数组中使用的次数。

$interfaceA = array($interfaceA_1,$interfaceA_2,$interfaceA_3,$interfaceA_4,$interfaceA_5,$interfaceA_6,$interfaceA_7,$interfaceA_8);       

$interfaceA_array=array_count_values($interfaceA);
$knappsatsA = $interfaceA_array[gui_knappsats];
$touchpanelA = $interfaceA_array[gui_touchpanel];

6 个答案:

答案 0 :(得分:11)

为什么不简单地创建一个新的javascript数组“计数” 迭代原始数组,并增加数组中遇到的键的“计数”计数。 http://jsfiddle.net/4t28P/1/

var myCurrentArray = new Array("apple","banana","apple","orange","banana","apple");

var counts = {};

for(var i=0;i< myCurrentArray.length;i++)
{
  var key = myCurrentArray[i];
  counts[key] = (counts[key])? counts[key] + 1 : 1 ;

}

alert(counts['apple']);
alert(counts['banana']);

答案 1 :(得分:6)

另一个优雅的解决方案是使用 Array.prototype.reduce 。 给出:

var arr = new Array("apple","banana","apple","orange","banana","apple");

您可以在其上运行 reduce

var groups = 
  arr.reduce(function(acc,e){acc[e] = (e in acc ? acc[e]+1 : 1); return acc}, {});

最后你可以查看结果:

groups['apple'];
groups['banana'];

在上面的示例中, reduce 有两个参数:

  1. 一个函数(匿名在这里)取一个累加器(从reduce的第二个参数初始化)和当前数组元素
  2. 累加器的初始值
  3. 无论函数返回什么,它都将在下一次调用中用作累加器值。

    从类型的角度来看,无论数组元素的类型如何,累加器的类型必须与reduce(初始值)的第二个参数的类型以及匿名函数的返回值的类型相匹配。 这也是 reduce 的返回值的类型。

答案 2 :(得分:2)

这个怎么样:

function arrayCountValues (arr) {
    var v, freqs = {};

    // for each v in the array increment the frequency count in the table
    for (var i = arr.length; i--; ) { 
        v = arr[i];
        if (freqs[v]) freqs[v] += 1;
        else freqs[v] = 1;
    }

    // return the frequency table
    return freqs;
}

答案 3 :(得分:0)

let snippet = "HARRY POTTER IS A SERIES OF FANTASY NOVELS WRITTEN BY BRITISH AUTHOR J. K. ROWLING. THE NOVELS CHRONICLE" +
    " THE LIVES OF A YOUNG WIZARD, HARRY POTTER , AND HIS FRIENDS HERMIONE GRANGER AND RON WEASLEY, ALL OF WHOM ARE " +
    " STUDENTS AT HOGWARTS SCHOOL OF WITCHCRAFT AND WIZARDRY";
    
String.prototype.groupByWord = function () {
    let group = {};
    this.split(" ").forEach(word => {
        if (group[word]) {
            group[word] = group[word] + 1;
        } else {
            group[word] = 1;
        }
    });
    return group;
};


let groupOfWordsByCount = snippet.groupByWord();
console.log(JSON.stringify(groupOfWordsByCount,null, 4))

答案 4 :(得分:0)

尝试

a.reduce((a,c)=> (a[c]=++a[c]||1,a) ,{});

let a= ["apple","banana","apple","orange","banana","apple"];

let count= a.reduce((a,c)=> (a[c]=++a[c]||1,a) ,{});

console.log(count);

答案 5 :(得分:0)

这应该工作

function array_count_values(array) {
  var tmpArr = {};
  var key = '';
  var t = '';
  var _countValue = function(tmpArr, value) {
    if (typeof value === 'number') {
      if (Math.floor(value) !== value) {
        return;
      }
    } else if (typeof value !== 'string') {
      return;
    }
    if (value in tmpArr && tmpArr.hasOwnProperty(value)) {
      ++tmpArr[value];
    } else {
      tmpArr[value] = 1;
    }
  }

  for (key in array) {
    if (array.hasOwnProperty(key)) {
      _countValue.call(this, tmpArr, array[key]);
    }

  }

  return tmpArr;
}
console.log(array_count_values([12, 43, 12, 43, "null", "null"]));