3

I am trying to perform a simple indexOf on my array the thing is that it just looks for the entire text within my array node

var arr = new Array();
arr.push("hello world");
var result = arr.indexOf("hello");

my problem is that the result I get is -1 is there any way to get the indexOf to search within each of the array element without another loop?

thanks

0

6 Answers 6

8

No, its not possible to use arr.indexOf to search for substring, you have to use loop.

Although you can add method to array to call it to find the substring in array.

Live Demo

Function definition.

arr.containsIndexOf = function(str) { 
    for(var i = 0; i < arr.length; i++){
     if(arr[i].indexOf(str) != -1)
         return i;
    }
    return -1;
}

Function call

arr.containsIndexOf("hello")
Sign up to request clarification or add additional context in comments.

Comments

1

Like Adil said you need to use a loop.

var myArr= new Array();
myArr.push("hello world");
function searchArr(txt, arr) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].toLowerCase().indexOf(txt.toLowerCase()) > -1) {
            return i
        }
    }
}

use this function like this

searchArr("hello",myArr); //returns 0

You might have a array with multiple "hello", so you need to know where all of them are.

myArr.push("hello world")
myArr.push("hello Pluto");
myArr.push("hi sun");
myArr.push("Hello Moon");
function searchArr(txt, arr) {
var arrList = new Array();
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].toLowerCase().indexOf(txt.toLowerCase()) > -1) {
           arrList.push(i);
        }
    }
return arrList
}
searchArr("hello",myArr); //returns [0, 1, 3]

1 Comment

You may like to return -1 in your first function (the simpler one) in case the search item was not found in the array.
1

As @Adil said, No you can't use indexOf function of Array to find substring but you can use indexOf of String to find substring:

Example:

var mystr = 'hello world';
var index = mystr.indexOf('hello');

With your example you should try somethings like:

var arr = new Array();
arr.push("hello world");
var mystr = arr[0]; // this is String object
var index = mystr.indexOf("hello");

Documentation:
string.indexOf
array.indexOf

Comments

0

Try this:

var arr = new Array();
arr.push("hello world");
var result = arr[0].indexOf("hello");

Because this is an Array so you need to access it with it's index.

Comments

0

You're pushing the whole of the string hello world into one index of the array so indexOf will only work when you use as it needs to match the whole item in that array index.

var result = arr.indexOf("hello world");

One way to do it would be like this:

var arr = new Array();
arr.push("hello world");

for(var i = 0; i < arr.length; i++){
     if(arr[i].indexOf('hello') != -1)
         return i;
}

See this jsfiddle

Comments

0
// Get index of the first matching item in an array
// or -1 if the item is not found
function SearchArray(arr, txt) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].indexOf(txt) > -1) {
            return i;
        }
    }
    return -1;
}

Function call:

var someArray = new Array();
someArray.push("hello world");

SearchArray(someArray, "hello");

Comments

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.