How to find the indexOf when using brackets?

It says

-1

in the console when doing this but I want to find the index of this square bracket.

let garage = [
['Ford', 'Grey', 2003],       ['Ford', 'Blue', 2004],        ['Ford', 'Orange', 2011],        ['Ford', 'Yello', 2015],        ['Ford', 'Red', 2018],
['Toyota', 'Gray', 2010],     ['Toyota', 'White', 2013],     ['Toyota', 'Dark blue', 2014],   ['Toyota', 'White', 2020],      ['Toyota', 'Gray', 2021],
['Honda', 'White', 2014],     ['Honda', 'White', 2015],      ['Honda', 'White', 2017],        ['Honda', 'White', 2018],       ['Honda', 'Blue', 2021],
['Lexus', 'Gray', 2012],      ['Lexus', 'Silver', 2013],     ['Lexus', 'White', 2015],        ['Lexus', 'Blue', 2018],        ['Lexus', 'Orange', 2020],
['Kia', 'White', 2014],       ['Kia', 'White', 2015],        ['Kia', 'Black', 2018],          ['Kia', 'Black', 2020],         ['Kia', 'Blue', 2021],
['Hyundai', 'Grey', 2015],    ['Hyundai', 'White', 2017],    ['Hyundai', 'Silver', 2018],     ['Hyundai', 'Silver', 2020],    ['Hyundai', 'White', 2021],
['Audi', 'Red', 2010],        ['Audi', 'White', 2012],       ['Audi', 'White', 2014],         ['Audi', 'White', 2017],        ['Audi', 'Silver', 2020],
['BMW', 'Blue', 2000],        ['BMW', 'Silver', 2003],       ['BMW', 'Orange', 2010],         ['BMW', 'Blue', 2015],          ['BMW', 'White', 2021],
['Mercedes', 'Silver', 2001], ['Mercedes', 'Silver', 2008],  ['Mercedes', 'Dark Gray', 2018], ['Mercedes', 'White', 2020],    ['Mercedes', 'Space Gray', 2021],
['Volvo', 'White', 2010],     ['Volvo', 'White', 2015],      ['Volvo', 'White', 2018],        ['Volvo', 'White', 2020],       ['Volvo', 'Dark Gray', 2021]
];
console.log(garage.indexOf(['Mercedes', 'Silver', 2001]));

you can’t, because an array is never equal to an other array. Just try [] === [], it will come out as false

you need a different method, like findIndex

this will become

garage.findIndex(function(el) {
   return el[0] === "Mercedes" && el[1] === "Silver" && el[2] === 2001
})
1 Like

Thank you for explaining.

make sure you famialiarize yourself with higher order functions, those are always around

1 Like

Is there any way I can find the index of a square bracket inside an array, with console.log() method?

I know now that indexOf doesn’t work, but I read the link you sent me and the code, but I didn’t succeed to find the index with console.log().

console.log can’t find the index, it just prints to the console

I suggested findIndex, as that returns the index of the first element that returns true from the callback function

if you use findIndex inside a console.log you will have the output printed to the console

1 Like
let garage = [
['Ford', 'Grey', 2003],       ['Ford', 'Blue', 2004],        ['Ford', 'Orange', 2011],        ['Ford', 'Yello', 2015],        ['Ford', 'Red', 2018],
['Toyota', 'Gray', 2010],     ['Toyota', 'White', 2013],     ['Toyota', 'Dark blue', 2014],   ['Toyota', 'White', 2020],      ['Toyota', 'Gray', 2021],
['Honda', 'White', 2014],     ['Honda', 'White', 2015],      ['Honda', 'White', 2017],        ['Honda', 'White', 2018],       ['Honda', 'Blue', 2021],
['Lexus', 'Gray', 2012],      ['Lexus', 'Silver', 2013],     ['Lexus', 'White', 2015],        ['Lexus', 'Blue', 2018],        ['Lexus', 'Orange', 2020],
['Kia', 'White', 2014],       ['Kia', 'White', 2015],        ['Kia', 'Black', 2018],          ['Kia', 'Black', 2020],         ['Kia', 'Blue', 2021],
['Hyundai', 'Grey', 2015],    ['Hyundai', 'White', 2017],    ['Hyundai', 'Silver', 2018],     ['Hyundai', 'Silver', 2020],    ['Hyundai', 'White', 2021],
['Audi', 'Red', 2010],        ['Audi', 'White', 2012],       ['Audi', 'White', 2014],         ['Audi', 'White', 2017],        ['Audi', 'Silver', 2020],
['BMW', 'Blue', 2000],        ['BMW', 'Silver', 2003],       ['BMW', 'Orange', 2010],         ['BMW', 'Blue', 2015],          ['BMW', 'White', 2021],
['Mercedes', 'Silver', 2001], ['Mercedes', 'Silver', 2008],  ['Mercedes', 'Dark Gray', 2018], ['Mercedes', 'White', 2020],    ['Mercedes', 'Space Gray', 2021],
['Volvo', 'White', 2010],     ['Volvo', 'White', 2015],      ['Volvo', 'White', 2018],        ['Volvo', 'White', 2020],       ['Volvo', 'Dark Gray', 2021]
];

garage.findIndex(function(el) {
  return el[0] === 'Mercedes' && el[1] === 'Silver' && el[2] === 2001
});

console.log(garage.length);



let OldCars = garage.splice(0,2);

console.log(garage);
console.log(OldCars);
console.log(garage.findIndex(['Mercedes', 'Silver', 2001]));

I did this, but I missunderstood what you meant.

you need a function inside findIndex

like I showed you before

1 Like

I have same code as you showed me, but where should I put it or, how do I make so the function gets connected to the array?

findIndex calls the function for each element of the array, the element is passed as first argument of the function inside findIndex

findIndex returns the index of first element for which the function returns true

1 Like

How do I connect findIndex() to the array and console.log()?

the reason why I want to get the index is so I know what position it is at when I splice it.

I don’t understand where to place the code.

in the way you have done with this and any other method, to use it on the garage array you write garage.findIndex(...)

findIndex returns a value, if you want to save it, store it in a variable
let myVar = garage.findIndex(...)

if you want to print it, put it in the console.log
console.log(garage.findIndex(...))

if you want to use it in splice to remove that element, use it garage.splice(garage.findIndex(...), 1)

passing a value around is one of the basic mechanics of JavaScript, you really need to have it clear to do anything meaningful

what did you want to do with indexOf? do the same but with findIndex (and appropriate argument)

1 Like

I want to find the index of ['Mercedes', 'Silver', 2001] in the garage array.

I’m getting error from this:

let garage = [
['Ford', 'Grey', 2003],       ['Ford', 'Blue', 2004],        ['Ford', 'Orange', 2011],        ['Ford', 'Yello', 2015],        ['Ford', 'Red', 2018],
['Toyota', 'Gray', 2010],     ['Toyota', 'White', 2013],     ['Toyota', 'Dark blue', 2014],   ['Toyota', 'White', 2020],      ['Toyota', 'Gray', 2021],
['Honda', 'White', 2014],     ['Honda', 'White', 2015],      ['Honda', 'White', 2017],        ['Honda', 'White', 2018],       ['Honda', 'Blue', 2021],
['Lexus', 'Gray', 2012],      ['Lexus', 'Silver', 2013],     ['Lexus', 'White', 2015],        ['Lexus', 'Blue', 2018],        ['Lexus', 'Orange', 2020],
['Kia', 'White', 2014],       ['Kia', 'White', 2015],        ['Kia', 'Black', 2018],          ['Kia', 'Black', 2020],         ['Kia', 'Blue', 2021],
['Hyundai', 'Grey', 2015],    ['Hyundai', 'White', 2017],    ['Hyundai', 'Silver', 2018],     ['Hyundai', 'Silver', 2020],    ['Hyundai', 'White', 2021],
['Audi', 'Red', 2010],        ['Audi', 'White', 2012],       ['Audi', 'White', 2014],         ['Audi', 'White', 2017],        ['Audi', 'Silver', 2020],
['BMW', 'Blue', 2000],        ['BMW', 'Silver', 2003],       ['BMW', 'Orange', 2010],         ['BMW', 'Blue', 2015],          ['BMW', 'White', 2021],
['Mercedes', 'Silver', 2001], ['Mercedes', 'Silver', 2008],  ['Mercedes', 'Dark Gray', 2018], ['Mercedes', 'White', 2020],    ['Mercedes', 'Space Gray', 2021],
['Volvo', 'White', 2010],     ['Volvo', 'White', 2015],      ['Volvo', 'White', 2018],        ['Volvo', 'White', 2020],       ['Volvo', 'Dark Gray', 2021]
];

let MS2001 = garage.findIndex(['Mercedes', 'Silver', 2001]);

console.log(garage.length);



let OldCars = garage.splice(0,2);

console.log(garage);
console.log(OldCars);
console.log(garage.indexOf(MS2001));

when you write this, do you mean to put in ['Mercedes', 'Silver', 2001] or any other square bracket?

I get this error

TypeError: [object Array] is not a function

let MS2001 = garage.findIndex(['Mercedes', 'Silver', 2001]);

This function

I don’t see the code for it.

Hi @amejl172

I think you need to go through the documentation for findIndex method. Essentially what @ilenia is saying is, you need to pass a function as an argument to findIndex and assign the returned value to a variable like:

const index = garage.findIndex(function(el) {
  return el[0] === 'Mercedes' && el[1] === 'Silver' && el[2] === 2001;
});

If there is ["Mercedes", "Silver", 2001] array in your garage then its index is index. If there is no ["Mercedes", "Silver", 2001] array in your garage then the value of index will be -1. If there are multiple arrays with the same entries, findIndex returns the index for the first occurrence .

After getting the index you can do whatever you want with it.

You can’t do something like garage.findIndex(['Mercedes', 'Silver', 2001]) because findIndex takes a function as an argument. That is why you are getting the TypeError error.

I hope that helps. You can read more about findIndex at MDN.

1 Like

They don’t show how to find the index of an array inside an array.

On the link they don’t cover arrays inside an array.

I don’t know how the function code looks, so I don’t how to build it.

An example that I want to understand is

const array1 = [['dog', 'fast', 1], ['dog', 'fast', 2], ['dog', 'slow', 3], ['dog', 'normal speed', 4], ['dog', 'turtle slow', 5]];

const Array = // I don't know how to make the function ;

console.log(array1.findIndex(Array));


You find the index of a nested array by using the content of the nested array. For example if you want to find the index of ['Kia', 'White', 2014] inside garage, you write the code like:

const indexOfNestedArray = garage.findIndex(function(nestedArr) {
  return  nestedArr[0] === 'Kia' && nestedArr[1] === 'White' && nestedArr[2] === 2014;
});
console.log(indexOfNestedArray);

Try running the above code.

1 Like

It worked, thanks for the help!

try to understand how it works, why it works and how to replicate it

you also had a similar example in your code

1 Like

I see now that you had to add it to a variable so you could see it in the console.

if you want to print something you need to put it inside console.log, this is valid for everything

1 Like