2

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.

1
  • Uh, words.indexOf(sources[0]) should work fine? What exactly did you try, where was said that what did not work? Commented May 8, 2015 at 2:37

4 Answers 4

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>

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot; it's working for me fine with what you've given. I'm accepting this answer and I've a small similar question in this only if you can help me with that as well. Will the above "indexOf" method work for objects as well? For example, instead of "words" array, if I've an object like nodes = {"id": 'Habitat for Humanity', "reflexive": true, "colour": "#007FFF", "radius": 70}; which stores every element of "words" array, would the same technique work on this too?
No, it will not. Take a look at this plugin (i'm the author, by the way), maybe you will find useful in some way to query Object's data: github.com/DiegoZoracKy/data-query
@PRANAVDASS If you need an example of code for doing that with a JSON and this plugin, create a new question or edit this one, so we can have a proper answer for that. Code formatting will help
3
sources.map(function(word) { return words.indexOf(word); })
// => [2, 7, 1, 4, 5]

Comments

1

You can use an each function.

$.each(words, function(key, value) {
      if(value == 'Helping'){
          alert(key);
      }
});

1 Comment

first think that came to mind... using this you can also get the value for more than just one value at a time.
0
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.

5 Comments

Is there a difference to what indexOf does?
Nothing. Its just that it's available from JavaScript version 1.6
Oh right, and that is available everywhere since when? 10 years ago?
indexOf also takes second input as from where to start searching
@Bergi just little bit less than 10 years :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.