php array_count_values用于JavaScript

umuewwlo  于 2023-09-29  发布在  PHP
关注(0)|答案(6)|浏览(118)

我有下面的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];
eeq64g8w

eeq64g8w1#

另一个优雅的解决方案是使用 * 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的第二个参数初始化)和当前数组元素
1.累加器的初始值
无论函数返回什么,它都将在下一次调用中用作累加器值。
从类型的Angular 来看,无论数组元素的类型如何,累加器的类型必须与reduce的第二个参数(初始值)的类型以及匿名函数的返回值的类型相匹配。这也将是 reduce 的返回值的类型。

xwmevbvl

xwmevbvl2#

尝试

array.reduce((a,c) => (a[c] = ++a[c] || 1, a) ,{});
let array = ["apple","banana","apple","orange","banana","apple"];

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

console.log(count);
lqfhib0f

lqfhib0f3#

不如这样吧:

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;
}
js5cn81o

js5cn81o4#

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))
798qvoo8

798qvoo85#

这个应该可以

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"]));
ymzxtsji

ymzxtsji6#

为什么不简单地创建一个新的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']);

相关问题