861 B
861 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |||
|---|---|---|---|---|---|---|---|---|
| Size of array, object or string | snippet | javascript |
|
digital-nomad-13 | 2023-01-20T13:25:47+03:00 |
Gets the size of an array, object or string.
- Get type of
val(array,objectorstring). - Use
Array.prototype.lengthproperty for arrays. - Use
lengthorsizevalue if available or number of keys for objects. - Use
sizeof aBlobobject created fromvalfor strings.
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