我是一个真正的初学者,也是 JavaScript 的爱好者。我需要帮助。我在使用下面的代码时遇到问题:
if (newTask.done === false) {
newTask = doneArray.push({
id: doneArray.length,
description: clickedTask.textContent,
done: false
});
} else {
removeTask = doneArray.splice(newTask.id, 1);
};
我需要检查我的对象在完成键中是否有假或真值。当新任务为 false 时,我想将其推送到我的数组,如果不是,则将其从我的数组中删除。我不知道如何获取 id 的值并在 if 语句 中使用它。然后将该对象中的值更改为true。我不知道,也许有更好的方法让它工作而不是删除整个代码。这是完整的代码: codepen.io
最佳答案
这是一个可以帮助您实现目标的简单方法。详情请查看评论,不清楚的请留言:
// 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);
关于javascript - 如何检查对象中键的值并在 if 语句中使用它? JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59670765/