JavaScript 统计出现次数

const data = ['car', 'van', 'truck', 'truck', 'bike', 'walk', 'car', 'van', 'walk', 'walk']
const transportation = data.reduce(function (obj, item) {
    if (!obj[item]) {
        obj[item] = 0
    }
    obj[item]++;

    return obj;
}, {})
console.table(transportation)

JavaScript 统计出现次数_第1张图片

 

你可能感兴趣的:(#,javascript,javascript,开发语言,ecmascript)