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 回答