javascript - 数组方法练习

这个练习我需要帮助:

数组方法

实现 uncompletedNotes 函数,给定一个笔记数组,只返回未完成的笔记。如果存在的所有待办事项都将完成标志设置为真,则认为注释已完成。

这是我所做的:

function uncompletedNotes(notes) {

  let result = []

  notes.forEach(element => {
    if (element.todos.done == false) {
      result.push(element);
    }
  });

  return result;

}

const notes = [{
    id: 1,
    description: 'Workout program',
    todos: [{
        id: 1,
        name: 'Push ups - 10 x 3',
        done: false
      },
      {
        id: 2,
        name: 'Abdominals - 20 x 3',
        done: true
      },
      {
        id: 3,
        name: 'Tapis Roulant - 15min',
        done: true
      }
    ]
  },
  {
    id: 2,
    description: 'Front-end Roadmap',
    todos: [{
        id: 1,
        name: 'Learn HTML',
        done: true
      },
      {
        id: 2,
        name: 'Learn CSS',
        done: true
      },
      {
        id: 3,
        name: 'Learn JavaScript',
        done: true
      },
      {
        id: 4,
        name: 'Learn Angular',
        done: true
      }
    ]
  }
]

const notesInProgress = uncompletedNotes(notes);
console.log('All notes: ', notes);
console.log('Notes In Progress: ', notesInProgress);

但它在 Notes in Progress 中给了我一个空数组结果。

最佳答案

function uncompletedNotes(notes) {
    return notes.filter(note => note.todos.filter(todo => !todo.done))
}

https://stackoverflow.com/questions/71467854/

相关文章:

javascript - Prop 验证中缺少 react / Prop 类型

c++ - 没有非空函数的返回语句是否是未定义的行为,其中控制永远不会结束?

c++ - 为什么 deques 默认用作堆栈的底层容器,而 vectors 可以做到这一点?

vue.js - Vitest 与 Quasar 的集成

javascript - 如何根据属性值从单个对象数组创建多个对象数组

rust - 如何在 CLI 应用程序中处理 "./"、 "~/"和相关参数

html - 如何使用 html + Tailwind CSS 使表格可滚动

r - 识别匹配对并创建一个公共(public) key

javascript - 我将如何在 P5.js 的 Canvas 上的两个位置之间创建点?

reactjs - webpack-cli : Invalid options object. De