Reputation: 3880
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 :)
Upvotes: 2
Views: 1449
Reputation: 8515
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");
Upvotes: 3
Reputation: 1527
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");
Upvotes: 2