admin管理员组文章数量:1426637
I'm really confused by this. If I do something like this:
[1].slice(1)
it returns an empty array (in the chrome interactive console). But if I pare:
[1].slice(1) === []
it's always false. So my Question is, what does [1].slice(1) really return?
I'm really confused by this. If I do something like this:
[1].slice(1)
it returns an empty array (in the chrome interactive console). But if I pare:
[1].slice(1) === []
it's always false. So my Question is, what does [1].slice(1) really return?
- 1 What are you trying to do? There is nothing to slice at index 1 – Esailija Commented Aug 13, 2012 at 16:52
- writing a lispy to javascript piler and trying to translate (rest '(1)). – rabra Commented Aug 13, 2012 at 16:58
4 Answers
Reset to default 8===
pares objects by references.
You're paring two different array objects which are both empty.
If you want to check whether an array is empty, check whether .length === 0
.
That's not a problem of slice
or ===
.
If you do [1]==[1]
, it returns false
.
That's because both ==
and ===
pare objects by reference
[] === []
also returns false. [1].slice(1)
does in fact return []
You better check the length:
[1].slice(1).length; // falsey
本文标签: javascriptArrayslice on array with one elementStack Overflow
版权声明:本文标题:javascript - Array.slice on array with one element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745480033a2660133.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论