Home > Software engineering >  Javascript array.findIndex(x) TypeError: x is not a function [closed]
Javascript array.findIndex(x) TypeError: x is not a function [closed]

Time:10-03

I am receiving the error `Uncaught TypeError: Lemon is not a function online with the following code

var guildList = [];
guildList.push("Lemon");
console.log(guildList); //some debugging code - I can see Lemon here
console.log(guildList.findIndex("Lemon"));

returns the error:

Uncaught TypeError: Lemon is not a function

I have attempted to check to make sure that the variable is a value (this case "Lemon"), however, this does not mitigate it.

CodePudding user response:

findIndex accepts a function. You are passing it a string.

Instead, pass a function which checks whether the parameter is equal to "Lemon":

function a() {
  var guildList = [];
  guildList.push("Lemon");
  console.log(guildList);
  return guildList.findIndex(e => e == "Lemon");
}

console.log(a())

Or use indexOf instead:

function a() {
  var guildList = [];
  guildList.push("Lemon");
  console.log(guildList);
  return guildList.indexOf("Lemon");
}

console.log(a())

  • Related