admin管理员组文章数量:1290433
I've 2 1-D arrays like this:
var words = ['Habitat for Humanity', 'Caring', 'Helping', 'People', 'Safety', 'Security', 'Shelter', 'Community', 'Volunteering'];
var sources = ['Helping', 'Community', 'Caring', 'Safety', 'Security'];
I want to find out the index of an element taken from the 'sources' array in the 'words' array. For example, the element 'Helping' in the 'sources' array is located at index 2 in 'words' array. So I need "2" as my solution when I search for index of the word "Helping". Can somebody help me on this? Thanks.
I tried using 'indexOf' function in arrays but id didn't work and when I searched for similar questions, it's been said that it'll not work in this problem.
I've 2 1-D arrays like this:
var words = ['Habitat for Humanity', 'Caring', 'Helping', 'People', 'Safety', 'Security', 'Shelter', 'Community', 'Volunteering'];
var sources = ['Helping', 'Community', 'Caring', 'Safety', 'Security'];
I want to find out the index of an element taken from the 'sources' array in the 'words' array. For example, the element 'Helping' in the 'sources' array is located at index 2 in 'words' array. So I need "2" as my solution when I search for index of the word "Helping". Can somebody help me on this? Thanks.
I tried using 'indexOf' function in arrays but id didn't work and when I searched for similar questions, it's been said that it'll not work in this problem.
Share Improve this question edited May 8, 2015 at 2:30 Amadan 198k23 gold badges251 silver badges319 bronze badges asked May 8, 2015 at 2:28 PRANAV DASSPRANAV DASS 411 silver badge5 bronze badges 1-
Uh,
words.indexOf(sources[0])
should work fine? What exactly did you try, where was said that what did not work? – Bergi Commented May 8, 2015 at 2:37
4 Answers
Reset to default 5Yes, indexOf will do the job. Check:
var words = ['Habitat for Humanity', 'Caring', 'Helping', 'People', 'Safety', 'Security', 'Shelter', 'Community', 'Volunteering'];
var sources = ['Helping', 'Community', 'Caring', 'Safety', 'Security'];
sources.forEach(function(source){
console.log(source, 'is at position', words.indexOf(source));
});
var words = ['Habitat for Humanity', 'Caring', 'Helping', 'People', 'Safety', 'Security', 'Shelter', 'Community', 'Volunteering'];
var sources = ['Helping', 'Community', 'Caring', 'Safety', 'Security'];
sources.forEach(function(source){
document.getElementsByTagName('div')[0].innerHTML += source + ' is at position: ' + words.indexOf(source) + '<br>';
});
<div></div>
sources.map(function(word) { return words.indexOf(word); })
// => [2, 7, 1, 4, 5]
You can use an each
function.
$.each(words, function(key, value) {
if(value == 'Helping'){
alert(key);
}
});
var searchArray=function(value,words){
var result=-1;
for(var i=0;i<words.length;i++){
if(words[i]==value){
result=I;
break
}
return result;
}
Answered From mobile. Feel free to format.
本文标签: find index of array element in another array javascriptStack Overflow
版权声明:本文标题:find index of array element in another array javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741439726a2378842.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论