我会写一些东西来计算所有类型
var obj = {
k1: ['a','b'], k2: ['cd','ef'],
k3: 0, k4: 1,
k5: {a:'b'},
k6: new Date(),
k7: "foo"
};
function getType(obj) {
var type = Object.prototype.toString.call(obj).slice(8, -1);
if (type === 'Object') return obj.constructor.name;
return type;
}
function countTypes(obj) {
var k, hop = Object.prototype.hasOwnProperty,
ret = {}, type;
for (k in obj) if (hop.call(obj, k)) {
type = getType(obj[k]);
if (!ret[type]) ret[type] = 1;
else ++ret[type];
}
return ret;
}
var theTypes = countTypes(obj);
// Object {Array: 2, Number: 2, Object: 1, Date: 1, String: 1}
现在,如果我只想知道数组的数量
var numArrays = theTypes.Array || 0; // 2 Arrays in this example