Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 138192

Why is my splice method deleting all objects past the selected index? (javascript)

$
0
0

CODE:

const todo = [{
    task:'Learn JS',
    priority: 1
},{
    task:'Make dinner',
    priority: 0
},{
    task:'Get sleep',
    priority: 2
},{
    task:'Free time',
    priority: 0
},{
    task:'Look for jobs',
    priority: 3
},{
    task: 'Have a healthy relationship',
    priority: 2
}]

// My delete function 
const deleteTodo = function (obj, title){
    return obj.splice(obj.findIndex(function (obj){
            return obj.task === title
        }, 1))
}
// Test to prove that my todo.findIndex is working
console.log(todo.findIndex(function (todo){
    return todo.task === 'Free time'
}))
// Printing out what I'm deleting
console.log(deleteTodo(todo, 'Free time'))
// Reprinting the todo list
console.log(todo)

NODE:

3
[
  { task: 'Free time', priority: 0 },
  { task: 'Look for jobs', priority: 3 },
  { task: 'Have a healthy relationship', priority: 2 }
]
[
  { task: 'Learn JS', priority: 1 },
  { task: 'Make dinner', priority: 0 },
  { task: 'Get sleep', priority: 2 }
]

So, given that my todo.findIndex is working, what is going wrong with my todo.splice? I set up a test todo.findIndex to show that it finds just index 3. So, inside the todo.splice function, the todo.findIndex should also equal 3, right? I've tried to reduce the amount of items that get deleted and even removing that number entirely. I don't know what I'm doing wrong.


Viewing all articles
Browse latest Browse all 138192

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>