获取数组,对象或字符串的大小。
获取 value
(array
,object
或 string
)的类型。
对于数组使用 length
属性。
对于对象,使用 size
(如果可用的话)或 length
的值,或者使用 Object.keys(value).length
来获取对象的键的数量。
对于字符串,使用根据 value
创建的 的 size
。
通过 split('')
将字符串拆分成字符数组并返回其长度。
const size = value => Array.isArray(value) ? value.length : value && typeof value === 'object' ? value.size || value.length || Object.keys(value).length : typeof value === 'string' ? new Blob([value]).size : 0;
size([1, 2, 3, 4, 5]); // 5 size('size'); // 4 size({ one: 1, two: 2, three: 3 }); // 3
更多代码 JavaScript 实用代码片段 请查看 https://www.7psus5.com/30-seconds-of-code/
最新评论
写的挺好的
有没有兴趣翻译 impatient js? https://exploringjs.com/impatient-js/index.html
Flexbox playground is so great!
感谢总结。
awesome!
这个好像很早就看到类似的文章了
比其他的教程好太多了
柯理化讲的好模糊…没懂