【问题标题】:how to return people from an array born between certain ages with a function如何使用函数从特定年龄段出生的数组中返回人
【发布时间】:2022-08-22 17:12:28
【问题描述】:

我对 js 很陌生,我正在尝试使用 .filter 的函数过滤掉出生在特定年龄之间的人

const allCoders = [
  {name: \'Ada\', born: 1816},
  {name: \'Alice\', born: 1990},
  {name: \'Susan\', born: 1960},
  {name: \'Eileen\', born: 1936},
  {name: \'Zoe\', born: 1995},
  {name: \'Charlotte\', born: 1986},
]

const findCodersBetween = (coders, startYear, endYear) => {
  allCoders.filter(coders = born > startYear && born > endYear)
  return coders
}
  • filter 函数需要一个函数作为它的参数,你传递的是一个布尔值

标签: javascript arrays function


【解决方案1】:

returnfunctionfilter 进行了小的更正

const findCodersBetween = (coders, startYear, endYear) => {
  return coders.filter((coder) = coder.born > startYear && coder.born < endYear);
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    相关资源
    最近更新 更多