是否可以再减少我的代码?

Is it possible to reduce my code anymore?

这是我的代码。

function legalPeopleToDrive(people) {
  const myFilter = people
    .filter(p => p.age > 14)
    .map(p => {
      return p.name;
    });
  return myFilter;
}

const myObj = [
  { name: "John", age: 14 },
  { name: "Jim", age: 16 },
  { name: "Ben", age: 18 }
];

console.log(legalPeopleToDrive(myObj));

差不多,我只是很好奇是否有可能再减少它。这也可以通过使用这些方法之一来完成 filter()map()?

您可以通过仅遍历数组一次来创建所需的结果,这可以通过 .reduce 完成。您也可以立即 return 数组方法的调用 - 无需事先将其存储在变量中。另一种选择是使用箭头函数来简洁 return:

const legalPeopleToDrive = people => people.reduce((a, p) => {
  if (p.age > 14) a.push(p.name);
  return a;
}, []);

const myObj = [
  { name: "John", age: 14 },
  { name: "Jim", age: 16 },
  { name: "Ben", age: 18 }
];

console.log(legalPeopleToDrive(myObj));

不过,您当前的代码对我来说看起来还不错。

答案:

最后回答你的问题,"Can this be done also by just using one of these methods filter() or map()?":是的 - 它只能由 filtermap 之一完成,但是,它不会按预期使用 filtermap,在这种情况下鼓励使用 reduceforEach

const legalPeopleToDrive = people => {
  const a = []; 
  people.map(p => {
    if (p.age > 14) {
      a.push(p.name);
    }
  });
  return a;
}

// An uber-short form would be:
// let legalPeopleToDrive = (people, a = []) => (people.map(p => p.age > 14 && a.push(p.name)), a);

const myObj = [
  { name: "John", age: 14 },
  { name: "Jim", age: 16 },
  { name: "Ben", age: 18 }
];

console.log(legalPeopleToDrive(myObj));

性能注意事项:

此页面上的一些其他想法可能会在大型集合或多次迭代的情况下导致更高的延迟;一般来说,避免在迭代中创建新数组,这是 flatMap()concat() 正在做或强迫你做的。用 filter()map() 扫描数组两次也比 forEach(), for 循环和 reduce() 没有 concat().[=26= 差]

const funcs = [
  "people => people.flatMap(p => p.age > 14 ? [p.name] : []);",
  "people => people.reduce((a, p) => p.age > 14 ? a.concat(p.name) : a, []);",
  "people => {let a = []; people.map(p => p.age > 14 && a.push(p.name)); return a;};",
  "people => people.filter(p => p.age > 14).map(p => p.name);",
  "people => {let a = []; for (let p of people) p.age > 14 && a.push(p.name); return a;};",
  "people => people.reduce((a, p) => (p.age > 14 && a.push(p.name), a), []);",
  "people => {let a = []; people.forEach(p => p.age > 14 && a.push(p.name)); return a;};",
  "people => [...(function*(people){for (let p of people) if (p.age > 14) yield p.name;})(people)];",
].sort(_ => .5 - Math.random());

const myObj = [
  { name: "John", age: 14 },
  { name: "Jim", age: 16 },
  { name: "Ben", age: 18 }
];

for (let f of funcs) {
  const legalPeopleToDrive = eval(f);
  const start = new Date();
  for (let i=0;i<1000000;++i) legalPeopleToDrive(myObj);
  console.log(f, new Date() - start, 'ms'); //, legalPeopleToDrive(myObj));
}

这一次,您可以用更 concise/compact 的方式编写函数:

const legalPeopleToDrive = people => people.filter(p => p.age > 14).map(p => p.name);

Can this be done also by just using one of these methods filter() or map()?

不使用这些方法,但是:

const legalPeopleToDrive = people => people.flatMap(p => p.age > 14? p.name: []);

您也可以 return Array.prototype 而不是创建一个空数组,但那样会更长。

您也可以将其编写为 iterator/generator 函数。不是真正缩短代码的方法,但是通过纯粹的字符数,仍然比您的代码短:

function* legalPeopleToDrive(people) {
  for (let p of people) if (p.age > 14) yield p.name;
}

const myObj = [
  { name: "John", age: 14 },
  { name: "Jim", age: 16 },
  { name: "Ben", age: 18 }
];

console.log([...legalPeopleToDrive(myObj)]);

请不要忘记您的结果现在是迭代器,而不是数组。