Consider the following data:
const state = {
tasks: {
'ID1': {
name: "Go to shop",
completed: false,
},
'ID2': {
name: "Get bananas",
completed: true,
},
'ID3': {
name: "Get apples",
completed: false,
}
}
}
To retrieve only the tasks that have completed
set to true
the follwoing code can be used:
function getCompletedTasks(state) {
let tasks = {}
Object.keys(state.tasks).forEach((key) => {
let task = state.tasks[key]
if (task.completed) tasks[key] = task
})
return tasks
}
I was wondering if there's a better way than manually creating a new array
with let tasks = {}
? I've looked at map
but I'm not really sure this can help. I'm a newbie, just trying to understand if there's a cleaner better way.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…