val
(array
, object
or string
).Array.prototype.length
property for arrays.length
or size
value if available or number of keys for objects.size
of a Blob
object created from val
for strings.split('')
and return its length.
const size = val =>
Array.isArray(val)
? val.length
: val && typeof val === 'object'
? val.size || val.length || Object.keys(val).length
: typeof val === 'string'
? new Blob([val]).size
: 0;
size([1, 2, 3, 4, 5]); // 5 size('size'); // 4 size({ one: 1, two: 2, three: 3 }); // 3
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️