【问题标题】:Using a higher order function, return one object value if another value is true (JavaScript)使用高阶函数,如果另一个值为真,则返回一个对象值 (JavaScript)
【发布时间】:2018-08-21 12:42:24
【问题描述】:

我有一个对象:

const animals = [
    {name: 'Fluffy', species: 'cat'},
    {name: 'Crinkle', species: 'rabbit'},
    {name: 'Wally', species: 'dog'},
    {name: 'Roo', species: 'dog'},
    {name: 'Felix', species: 'cat'},
]

我想使用更高阶的函数,例如 filter() 方法来获取动物对象的数组并返回一个仅包含所有狗名称的数组,即["Wally", "Roo"]。我的代码目前返回一个数组,其中包含整个对象以及其中的物种狗。见下文:

const dogArray = animals.filter(function(animal) {
  return animal.species === 'dog';
 })

return dogArray;

// returns
// [{name: "Wally", species: "dog"}, 
// { name: "Roo", species: "dog"}]

【问题讨论】:

  • 在这里使用reduce 会很有效,因为您在同一个数组上进行过滤和映射。
  • 是的,filter 就是这样工作的。您还需要map 该结果。您可以使用 reduce 将这两个操作组合到同一个迭代中。
  • 这是一个合法的问题并且格式正确(嗯,第一个代码块中的缩进会有所帮助)。不知道你为什么投了反对票。

标签: javascript arrays object higher-order-functions


【解决方案1】:

只需将过滤后的数组的元素映射到它们的 name 属性:

const animals = [
  {name: 'Fluffy', species: 'cat'},
  {name: 'Crinkle', species: 'rabbit'},
  {name: 'Wally', species: 'dog'},
  {name: 'Roo', species: 'dog'},
  {name: 'Felix', species: 'cat'},
]

const dogArray = animals.filter(animal => animal.species === 'dog');

console.log(dogArray.map(dog => dog.name));

或者将两者合二为一:

const animals = [
  {name: 'Fluffy', species: 'cat'},
  {name: 'Crinkle', species: 'rabbit'},
  {name: 'Wally', species: 'dog'},
  {name: 'Roo', species: 'dog'},
  {name: 'Felix', species: 'cat'},
]

let dogArray = animals.reduce((dogs, animal) => {
  if (animal.species === "dog") dogs.push(animal.name);
  return dogs;
}, []);

console.log(dogArray)

【讨论】:

    【解决方案2】:

    您可以使用destructuring 映射属性。

    const
        animals = [{ name: 'Fluffy', species: 'cat' }, { name: 'Crinkle', species: 'rabbit' }, { name: 'Wally', species: 'dog' }, { name: 'Roo', species: 'dog' }, { name: 'Felix', species: 'cat' }]
        dogArray = animals
            .filter(({ species }) => species === 'dog')
            .map(({ name }) => name);
    
    console.log(dogArray);

    【讨论】:

      【解决方案3】:

      const animals = [
      {name: 'Fluffy', species: 'cat'},
      {name: 'Crinkle', species: 'rabbit'},
      {name: 'Wally', species: 'dog'},
      {name: 'Roo', species: 'dog'},
      {name: 'Felix', species: 'cat'},
      ]
      
      var result = animals.filter(val=>val.species=="dog").map(({name})=>name);
      
      console.log(result);

      【讨论】:

        【解决方案4】:

        您可以使用.filter() 后跟.map()

        const animals = [
          {name: 'Fluffy', species: 'cat'},
          {name: 'Crinkle', species: 'rabbit'},
          {name: 'Wally', species: 'dog'},
          {name: 'Roo', species: 'dog'},
          {name: 'Felix', species: 'cat'},
        ];
        
        const dogNames = animals
          .filter(animal => animal.species === 'dog')
          .map(dog => dog.name);
        
        console.log(dogNames);

        【讨论】:

          【解决方案5】:

          创建一个空数组,使用for 循环遍历现有的dogArray,将名称推送到新数组中,然后返回新数组。

          const dogArray = animals.filter(function(animal) {
            return animal.species === 'dog';
           })
          
          let dogNames = [];
          
          for (let i in dogArray) {
            dogNames.push(dogArray[i].name);
          }
          
          return dogNames;
          

          【讨论】:

          • 我很乐意解决投票者认为的任何问题。
          • 反对票可能来自于没有解释你的代码在做什么
          猜你喜欢
          • 2021-06-30
          • 2022-11-17
          • 1970-01-01
          • 2016-02-16
          • 2018-04-10
          • 2018-06-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多