admin管理员组文章数量:1278977
While I am writing some JavaScript, I came across a new code "indexOf". After read another post I thought its behaviour is as shown below but it seems not true. Can someone kindly give me an explanation about "indexOf", please?
false = -1; true = 0 and more?
I have tried to change -1 to 0 and more but then nothing happens. Just to have a better understanding about jquery/indexOf.
what I have now,
$(this).closest(row)[td_word.indexOf(keyword) !== -1 ? 'show' : 'hide']();
it search for match(es) of "keyword" from "td_word", if it is not false (!== -1, thus true) display:visible;, if it is not true (false) display:hide;.
Thanks in advance.
While I am writing some JavaScript, I came across a new code "indexOf". After read another post I thought its behaviour is as shown below but it seems not true. Can someone kindly give me an explanation about "indexOf", please?
false = -1; true = 0 and more?
I have tried to change -1 to 0 and more but then nothing happens. Just to have a better understanding about jquery/indexOf.
what I have now,
$(this).closest(row)[td_word.indexOf(keyword) !== -1 ? 'show' : 'hide']();
it search for match(es) of "keyword" from "td_word", if it is not false (!== -1, thus true) display:visible;, if it is not true (false) display:hide;.
Thanks in advance.
Share Improve this question edited Dec 22, 2017 at 1:19 Maki asked Feb 27, 2015 at 0:48 MakiMaki 6374 gold badges12 silver badges26 bronze badges 6-
2
So… what’s the question? Is it “why does
indexOf
return-1
sometimes? – Andrew Marshall Commented Feb 27, 2015 at 0:52 -
1
And of what relevance to JavaScript's
indexOf
is a prior question regarding C#??? – Lightness Races in Orbit Commented Feb 27, 2015 at 0:53 -
1
indexOf
is not a jQuery method. – Bergi Commented Feb 27, 2015 at 0:54 - Andrew, with the way you put it. I would then ask, what numbers can indexOf return? (-1, 0, 1) only or more? – Maki Commented Feb 27, 2015 at 0:59
- Lightness Races in Orbit, may I ask if you have a link for the bloomin' documentation you mentioned? I am not sure what you are talking about on C#. I tagged jquery only before Bergi edited. – Maki Commented Feb 27, 2015 at 1:01
1 Answer
Reset to default 10array.indexOf(element)
returns the index of the element in the array. Read the official documentation as well.
It was designed to return -1
when the element doesn't exist, because 0
would mean that the element is in the 0th index (1st element).
Examples :
var array = ['a','b','c','d','e'];
array.indexOf('a') //0
array.indexOf('c') //2
array.indexOf('f') //-1, because it doesn't exist in array
From what I understand in your wording, I think that you think that indexOf
is used to check if a certain element exists in an array. That is just a "side-effect" of indexOf
but its actual usage is getting the index of an element in the array.
本文标签: javascriptWhat does 0 mean in indexOfStack Overflow
版权声明:本文标题:javascript - What does 0 mean in indexOf? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741259558a2367347.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论