Javascript findIndex is not a function

javascript find index of object in array by property
findindex javascript
findindex is not a function react
data.findindex is not a function react native
javascript indexof
javascript findindex with object
angularjs find index of object in array by id
findindex vs indexof

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", "birthday":"1974/06/12"},
  {"id":21,"name":"Nicolai", "lastname":"GRAHAM", "hobby":"reading", "birthday":"2005/01/22"},
  {"id":22,"name":"Vasile", "lastname":"BRYANT", "hobby":"singing", "birthday":"1987/03/17"}
]

function to remove a item from json array

 removeItem: function(removeId){
   //paramater validation
   return dataLoad.then(function(data){

     f = data.findIndex(function(item) { return item.id == removeId; });

     if(f < 0)
       return false;
     data.splice(f,1);

     LS.setData(data,"cutomers");

     return true;
   });
 }

When the code is running there is an error:

findIndex is not a function

error line

 f = data.findIndex(function(item) { return item.id == removeId; });

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) {
    return false;
}

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) {
    return false;
}

data.splice(f, 1);

Found at Array.prototype.findIndex MDN

TypedArray.prototype.findIndex(), If it finds an array element where the function returns a true value, findIndex() returns the index of that array element (and does not check the remaining values)​  The findIndex() method returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test. The source for this interactive example is stored in a GitHub repository.

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.

Array.prototype.findIndex(), It does not change the original array. Syntax: array.findIndex(function(​currentValue, index, arr), thisValue). Parameters:. vue findIndex not a function. 3 Answers 3. Your state.carts is an object, not an Array. The findIndex() method returns the index of the first element in the array that satisfies the provided testing function.

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!

JavaScript Array findIndex() Method, Learn JavaScript ES6 — Array.find & Array.findIndex Find & Find Index Test must be provided as a function. find() method executes a callback If nothing passes, undefined is returned. find() does not mutate or change  JavaScript | Array.findIndex() Method. It returns index of the first element in a given array that satisfies the provided testing function. Otherwise -1 is returned. It does not execute the function once it finds an element satisfying the testing function. It does not change the original array.

JavaScript, If it finds the array element where the function returns the true value, then the findIndex() returns an index of that array element (and does not  Definition and Usage. The findIndex() method returns the index of the first element in an array that pass a test (provided as a function). The findIndex() method executes the function once for each element present in the array: Note: findIndex() does not execute the function for array elements without values.

Learn JavaScript ES6, If it finds an array element where the function returns a true value, findIndex() returns the index of that array element (and does not check the remaining values)​  I' am currently working with the following code. In the console it's throwing Uncaught TypeError: TotalAccountBalance.indexOf is not a function I don't know what else to do. Searching didn't h

Javascript Array FindIndex Example, The findIndex() method does not mutate the array on which it is called. Syntax: arr​.findIndex(callback[, thisArg]). Parameters. callback. Function  The indexOf() method returns the position of the first occurrence of a specified value in a string. This method returns -1 if the value to search for never occurs. Note: The indexOf() method is case sensitive. Tip: Also look at the lastIndexOf() method.

Comments
  • Maybe you should update your title to the question, as the question body doesn't mention indexof.
  • Did you do a console.log(data) before that line? More specifically, can you confirm that data is an Array?
  • Could you share the whole code? There is no enough information.
  • Joachim Pileborg, thnx for title :). And no i dinn`t console.log(data).
  • instead of filter, i would suggest Array.prototype.some because it stops the iteration if found.
  • Agreed, updating my answer accordingly
  • You answer gives the impression that filter will stop iteration when finding a match. This is not true - it is true for some though.
  • "There is a suggested polyfill att the MDN page that you can use if you want to use findIndex today." This was helpful! Thanks!
  • Unfortunately the polyfill returns the index as a string (at least in IE 11)