Reputation: 37
I'am a real beginner and just enthusiast of JavaScript. I need help. I've got problem with code below:
if (newTask.done === false) {
newTask = doneArray.push({
id: doneArray.length,
description: clickedTask.textContent,
done: false
});
} else {
removeTask = doneArray.splice(newTask.id, 1);
};
I need to check if my object have false or true value in done key. When new task have false I want to push it to my Array, and if not remove it from my Array. I don't know how to take value of id and use it in if statement. Then change that value in object into true. I don't know, maybe there is much better way to make it work and not deleting whole code. Here is full code: codepen.io
Upvotes: 0
Views: 1702
Reputation: 5941
Here is a simple approach that should help you achieve your goal. Check the comments for details and leave a comment if unclear:
// Here is an array containing 3 new tasks
const allTasks = [{
id: 0,
description: 'desc 1',
done: true
}, {
id: 1,
description: 'desc 2',
done: false
}, {
id: 2,
description: 'desc 3',
done: true
}];
// Here is an function to get a new array of only NOT DONE tasks
// We achieve this by simply using Array.prototype.filter method
const filterDoneTasks = arr => arr.filter(task => !task.done);
const pendingTasks = filterDoneTasks(allTasks);
console.log(pendingTasks);
Upvotes: 2
Reputation: 1
Try something like this. ..
if (newTask.done === false) {
newTask = doneArray.push({
id: doneArray.length,
description: clickedTask.textContent,
done: false
});
} else {
for(let i = 0; i < doneArray.length; ++i){
if(doneArray[i].id == newTask.id)
removeTask = doneArray.splice(i, 1);
break;
}
} };
However, every time you remove an object from the array the length should get smaller, causing multiple objects with the same ID. this means you will have to decide to do a remove first operation, or remove all operation for the array. I would use a different ID like a counter that always goes up, unless this is intentional.
edit-- it may also just be length starts at 1 and array index starts at 0. so you have to account for that if the wrong number is being removed.
Upvotes: 0