Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!