4

我有以下 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];
4

6 回答 6

13

为什么不简单地创建一个新的javascript数组“counts”迭代原始数组,并增加数组中遇到的键的“counts”计数。 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']);
于 2011-03-07T07:52:24.730 回答
7

另一个优雅的解决方案是使用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. 累加器的初始值

无论函数返回什么,它将在下一次调用中用作累加器值。

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

于 2014-07-27T11:10:43.810 回答
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;
}
于 2011-03-07T08:01:08.560 回答
2

尝试

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

于 2019-07-08T07:13:49.987 回答
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))

于 2018-10-20T18:03:05.377 回答
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"]));

于 2020-06-08T17:50:06.900 回答