我有数组,
$servArray = [AC Service,AC Installation,AC Service, AC Installation];
所以我想打印
AC Service = 2;
AC Installation = 2
如何打印两个值。
谢谢。
答案 0 :(得分:1)
答案 1 :(得分:0)
我们可以使用es6 map
函数并迭代数组并在声明的对象中分配期望的结果。
const $servArray = ['AC Service','AC Installation','AC Service', 'AC Installation'];
const counts = {};
$servArray.map(x => counts[x] = (counts[x] || 0)+1);
console.log(counts);