Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

220
Vistas
TypeError: Cannot read property '1' of undefined (JavaScript)

I'm making some functions that loop through my 2D array and check if the value in an index is matching the parameter I passed. But I keep getting this error of "TypeError: Cannot read property '1' of undefined at findBookByTitle"

Any suggestions, I have tried everything :(

const BooksInfo = [];

function addBook(BookID, BookTitle, Author, Price, Quantity){

    let newBook = [BookID,BookTitle,Author,Price,Quantity]
    BooksInfo.push(newBook);
}

addBook(1,"Start with Why","Simon Sinek", 80.0, 13);
addBook(2, "But how do it know", "J. Clark Scott", 59.9, 22);
addBook(3, "Clean Code", "Rober Cecil Martin", 50.0, 5);
addBook(4, "Zero to One", "Peter Thiel", 45.0, 12);
addBook(5, "You don't know JS", "Kyle Simpson", 39.9, 9);

//console.log(BooksInfo);

function findBookByID(BookID){
    for(i=0; i <= BooksInfo.length; i++){
        if(BooksInfo[i][0] == BookID){
            console.log(BooksInfo[i])
        }
    }
}

function findBookByTitle(BookTitle){
    for(i=0; i <= BooksInfo.length; i++){
        if(BooksInfo[i][1] == BookTitle){
            console.log(BooksInfo[i])
        }
    }
}

function findBookByAuthor(Author){
    for(i=0; i <= BooksInfo.length; i++){
        if(BooksInfo[i][2] == Author){
            console.log(BooksInfo[i])
        }
    }
}

findBookByAuthor("Kyle Simpson");
findBookByID(1);
findBookByTitle("But how do it know");


about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

function findBookByTitle(BookTitle){
    for(i=0; i < BooksInfo.length; i++){
        if(BooksInfo[i][1] == BookTitle){
            console.log(BooksInfo[i])
        }
    }
}

since you're looping from index 0 to the last index, can't include the last number in the booksInfo.length. e.g: here, your booksInfo.length = 5, looping from index 0 to 5, is 6 times the iteration for i(0,1,2,3,4,5)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Make your life so much easier (and your code so much more concise and readable) by using objects instead of representing each book in an array:

const BooksInfo = [];

function addBook(BookID, BookTitle, Author, Price, Quantity){
    let newBook = {BookID,BookTitle,Author,Price,Quantity};
    BooksInfo.push(newBook);
}

addBook(1,"Start with Why","Simon Sinek", 80.0, 13);
addBook(2, "But how do it know", "J. Clark Scott", 59.9, 22);
addBook(3, "Clean Code", "Rober Cecil Martin", 50.0, 5);
addBook(4, "Zero to One", "Peter Thiel", 45.0, 12);
addBook(5, "You don't know JS", "Kyle Simpson", 39.9, 9);

function findBookByID(BookID){
    return BooksInfo.find(book => book.BookID === BookID);
}

function findBookByTitle(BookTitle){
    return BooksInfo.find(book => book.BookTitle === BookTitle);
}

function findBookByAuthor(Author){
    return BooksInfo.find(book => book.Author === Author);
}

console.log(findBookByAuthor("Kyle Simpson"));
console.log(findBookByID(1));
console.log(findBookByTitle("But how do it know"));

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda