Home > Net >  TypeError: Cannot read property '1' of undefined (JavaScript)
TypeError: Cannot read property '1' of undefined (JavaScript)

Time:03-07

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");


CodePudding user response:

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)

CodePudding user response:

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"));

  • Related