2

Hi i had write a test function to remove object from array base on the property value(title)

I have the books array which contains many book object inside of it, currently whenever i run the remove function it should remove the object from the array that contain a title.

First time i run it success in deleting it, But if i try to run the function more than one time then it still gonna delete the other even if the title of the book object not even matching.

im running this js file with nodeJS

const books = [
    {
        title: 'Harry Potter and the Chamber of Secrets',
        author: "abc",
    },
    {
        title: 'Jurassic Park',
        author: "cde",
    },
];

const add = (titleName, authorName) => {
    newBook = {
        title: titleName,
        author: authorName
    }
    books.push(newBook)

   console.log(books)
};

const remove = (titleName) => {
    bookToDelete = books.findIndex(book => book.title === titleName);
    books.splice(bookToDelete, 1);
    console.log(books)
}

// add an item with title = abc
add("abc", "linh");

// remove book with title = abc first time
remove("abc");

// remove book second time, this shouldn't work but the last element title not abc still got deleted
remove("abc");

Hope someone tell me what wrong with my code :)

3
  • 4
    that's because -1 is being passed into bookToDelete in books.splice(bookToDelete, 1); Commented Nov 25, 2019 at 8:19
  • 1
    isn't the second parameter of splice() is how many item to delete from array ? sorry i'm still kinda new to javascript. If so than is there anyway i can fix my remove() function ? Commented Nov 25, 2019 at 8:21
  • 1
    @LinhNguyen the issue is findIndex returns -1 the second time. If the first parameter to splice is negative, splice will begin from the end of the array. Documentation Commented Nov 25, 2019 at 8:25

2 Answers 2

3

It's because -1 is being passed to books.splice() if no book found in the array. And from the MDN about the first argument (start) we read:

If negative, it will begin that many elements from the end of the array (with origin -1, meaning -n is the index of the nth last element and is therefore equivalent to the index of array.length - n)

and about the return value of the Array.findIndex() we read:

The index of the first element in the array that passes the test. Otherwise, -1.


So in your case, if a book is not found, you pass -1 so it deletes the first element from the end of the books array.

You can fix this, by checking if the bookToDelete is greater than -1. See example below:

const books = [
    {
        title: 'Harry Potter and the Chamber of Secrets',
        author: "abc",
    },
    {
        title: 'Jurassic Park',
        author: "cde",
    },
];

const add = (titleName, authorName) => {
    newBook = {
        title: titleName,
        author: authorName
    }
    books.push(newBook)

   console.log(books)
};

const remove = (titleName) => {
    bookToDelete = books.findIndex(book => book.title === titleName);
    
    if (bookToDelete > -1) { // if its false, it means that there's no such book
      books.splice(bookToDelete, 1);
      console.log(books)
    }
}

// add an item with title = abc
add("abc", "linh");

// remove book with title = abc first time
remove("abc");

// remove book second time, this shouldn't work but the last element title not abc still got deleted
remove("abc");

Sign up to request clarification or add additional context in comments.

1 Comment

Oh i didn't know that findIndex would return -1 if it doesn't find anything. Good to know :)
2

You don't need to use splice and findIndex. array.filter function would do it. You need to replace const to let at the first line.

let books = [
    {
        title: 'Harry Potter and the Chamber of Secrets',
        author: "abc",
    },
    {
        title: 'Jurassic Park',
        author: "cde",
    },
];

const add = (titleName, authorName) => {
    newBook = {
        title: titleName,
        author: authorName
    }
    
    books.push(newBook)
    console.log(books)
};

const remove = (titleName) => {
    books = books.filter(book => book.title !== titleName);
    console.log(books)
}

// add an item with title = abc
add("abc", "linh");

// remove book with title = abc first time
remove("abc");

// remove book second time, this shouldn't work but the last element title not abc still got deleted
remove("abc");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.