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
Add a ment  | 

4 Answers 4

Reset to default 5

Yes, 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