0

I have a JS array made with jQuery toArray() method containing all li elements from a div. Now I try to get the index of the array containing a certain string: showing.

I have tried .index, .filter, but it all gives me the same error message,

arraySlides.index is not a function
logic.js:218 Uncaught TypeError: showing is not a function

This is my array:

[li.slide.showing, li.slide, li.slide]
0:li.slide.showing
1:li.slide
2:li.slide
length:3
__proto__:Array(0)

Is it not working because I try to find a string?

var current = arraySlides.index('showing');

Ok maybe I wasn't clear. I am trying to get the index, so a number from which position in the array the classname contains showing..

1
  • The method is called indexOf, not index. However, filter is an extant method, so I'm surprised that you see a not-a-function error for that, unless you're using an old browser. Commented Apr 26, 2017 at 19:08

4 Answers 4

4

I don't believe you're seeing filter is not a function. You're probably seeing something more like showing is not a function.

[].filter('showing')

That's because filter accepts a predicate function. You need to pass a function which returns true for the items you want. Here's what you probably want:

let listItems = $('li').toArray();
let showing = listItems.filter(li => li.classList.contains('showing'));
console.log(showing);

// Or if you want to get it out of the array
console.log(showing[0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>hidden</li>
  <li>hidden</li>
  <li class="showing">showing</li>
  <li>hidden</li>
  <li>hidden</li>
</ul>

Rewritten in ES5:

var listItems = $('li').toArray();
var showing = listItems.filter(function(li) {
  return li.classList.contains('showing');
});
console.log(showing);

// Or if you want to get it out of the array
console.log(showing[0]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>hidden</li>
  <li>hidden</li>
  <li class="showing">showing</li>
  <li>hidden</li>
  <li>hidden</li>
</ul>

If you want the index of an element, you can use findIndex which also takes a predicate method:

var listItems = $('li').toArray();
var showingIndex = listItems.findIndex(function(li) {
  return li.classList.contains('showing');
});
console.log(showingIndex);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li>hidden</li>
  <li>hidden</li>
  <li class="showing">showing</li>
  <li>hidden</li>
  <li>hidden</li>
</ul>

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

3 Comments

Thanks for this extensive answer, but I needed to search classnames, like in the answer below. Still giving you some points because this answer is rock solid
@HuubS Updated. Includes how to get the index towards the end.
Awesome, but I get the classname out of it, is it also possible to get the position in the array (so 1, 2, etc)?
2

You should be using the indexOf() Method.

var current = arraySlides.indexOf('showing');

Just so you know, the indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present. The syntax is like below

arr.indexOf(searchElement) 

or

arr.indexOf(searchElement, fromIndex)

Comments

1

var arr = $('li').toArray();
console.log(arr);

console.dir(searchStringInArray('showing',arr));

function searchStringInArray (str, strArray) {
    for (var j=0; j<strArray.length; j++) {
        if ( String(strArray[j]).indexOf(str) ) return strArray[j];
    }
    return -1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="slide showing">One</li>
<li class="slide">Two</li>
<li class="slide">Three</li>

https://jsfiddle.net/HappyiPhone/exbnpvh1/

Comments

0

I am just providing you a way to get the index of the elements of your array containing the substring showing, since the other answers here seem to be giving you a way of searching for the whole string in array.

var values = ["li.slide", "li.slide.showing", "li.slide"];

$.each(values, function(index, value) {
	if(value.includes('showing')) {
            console.log("Found at index:"+index);
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

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.