Skip to content Skip to sidebar Skip to footer

Javascript Findindex Is Not A Function

I have a json array: [ {'id':19,'name':'Jed', 'lastname':'DIAZ', 'hobby':'photo', 'birthday':'2011/11/22'}, {'id':20,'name':'Judith', 'lastname':'HENDERSON', 'hobby':'pets'

Solution 1:

findIndex is not a prototype method of Array in ECMASCRIPT 262, you might need filter combined with indexOf, instead, it has the advantage of stopping searching as soon as entry is found

var f;
var filteredElements = data.filter(function(item, index) { f = index; return item.id == removeId; });


if (!filteredElements.length) {
    returnfalse;
}

data.splice(f, 1);

EDIT as suggested in comments by Nina Scholz:

This solution is using Array.prototype.some instead

var f;
var found = data.some(function(item, index) { f = index; return item.id == removeId; });

if (!found) {
    returnfalse;
}

data.splice(f, 1);

Found at Array.prototype.findIndex MDN

enter image description here

Solution 2:

I think you'll find your answer in your own headline; you're probably looking for indexOf, instead of findIndex. findIndex is a part of ECMAScript2015 and isn't very widely supported yet.

According to MDN, findIndex is only supported in Firefox 25+ and Safari 7.1+, so if you're testing in any other browser you'd get the error you're having.

There is a suggested polyfill att the MDN page that you can use if you want to use findIndex today.

Solution 3:

You can use Polyfill as IE do not support Array.prototype.findIndex()

I have tested and it worked for me.

MDN has a polyfill for this.

Hope this helps!

Post a Comment for "Javascript Findindex Is Not A Function"