I was trying to make a javascript code that searches inside an objects array for text and then deletes the whole index (the index that includes the text property) but the code failed and always return 'undefined' so i wanted to get some help.
const todos = [{
text: 'wake up',
completed: true
}, {
text: 'get some food',
completed: false
}, {
text: 'play csgo',
completed: false
}, {
text: 'play minecraft',
completed: true
}, {
text: 'learn javascript',
completed: false
}]
let todo = function (todo, todoText) {
return todo.find(function (text, index) {
if (todo.text.toLowerCase() === todoText.toLowerCase()) {
todo.splice(index, 1);
}
})
}
let deleteTodo = todo(todos, 'wake up');
console.log(deleteTodo);
i was expecting this output:
[{
text: 'get some food',
completed: false
}, {
text: 'play csgo',
completed: false
}, {
text: 'play minecraft',
completed: true
}, {
text: 'learn javascript',
completed: false
}]
but the output was actually 'undefined'
Array.prototype.find()but your callback doesn't return anything sofind()finds nothing (ieundefined)todo.textshould betext.text