0

var array = ['box','box','line','time','box']

例:ボックスは数回繰り返されます

4

1 に答える 1

0

どうぞ:

    var array = ['box','box','line','time','box'],
    map = {},
    key;

array.forEach(function(item) {
    if (map[item] === undefined) {
        map[item] = 1;
    } else {
        map[item]++;
    }
});

for (key in map) {
    console.log(" key:", key, " repeated:", map[key]);
}
于 2013-09-21T08:29:34.250 に答える