Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
54
rated 0 times [  57] [ 3]  / answers: 1 / hits: 29769  / 9 Years ago, tue, may 26, 2015, 12:00:00

I looked it up and found this regarding finding a substring in a larger string in an array. Array.Prototype.includes



if (t.title.includes(searchString))


My t is part of a $.each that's iterating through a larger array of objects (each objects got a buttload of info, from strings, dates and such). searchString is whatever the user typed in a box. All this is a simple search function for a list I have on the page.



This works just fine in Chrome. But Firefox and IE are turning up errors stating



TypeError: currentTicket.title.includes is not a function


So I either put up a warning sign that my app only works on Chrome or I handcraft a find function? Weird thing is the doc page from MDN I posted states that only Firefox supports the array.includes, strangely enough it's only Chrome that runs it.


More From » arrays

 Answers
49

Instead of using an API that is currently marked as experimental consider using a more broadly supported method, such as Array.prototype.indexOf() (which is also supported by IE).



Instead of t.title.includes(string) you could use t.title.indexOf(string) >= 0



You can also use Array.prototype.filter() to get a new array of strings that meet a certain criteria, as in the example below.





var arr = [one, two, three, four, five, six, seven, eight, nine, ten];
document.getElementById(input).onkeyup = function() {
document.getElementById(output).innerHTML = arrayContainsString(arr,this.value);
}
document.getElementById(header).innerHTML = JSON.stringify(arr);

function arrayContainsString(array, string) {
var newArr = array.filter(function(el) {
return el.indexOf(string) >= 0;
});
return newArr.length > 0;
}

<input id=input type=text />
<br/>
<div>array contains text:<span id=output />
</div>
<div id=header></div>




[#66466] Saturday, May 23, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
karivictoriab

Total Points: 530
Total Questions: 90
Total Answers: 95

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
;