使用高阶函数,return 一个对象值,如果另一个值是 true (JavaScript)
Using a higher order function, return one object value if another value is true (JavaScript)
我有一个对象:
const animals = [
{name: 'Fluffy', species: 'cat'},
{name: 'Crinkle', species: 'rabbit'},
{name: 'Wally', species: 'dog'},
{name: 'Roo', species: 'dog'},
{name: 'Felix', species: 'cat'},
]
我想使用高阶函数,例如 filter() 方法来获取动物对象数组,return 一个只包含所有狗的名字的数组,即 ["Wally", "Roo"]
。我现在的代码 return 是一个包含整个对象的数组,里面有狗。见下文:
const dogArray = animals.filter(function(animal) {
return animal.species === 'dog';
})
return dogArray;
// returns
// [{name: "Wally", species: "dog"},
// { name: "Roo", species: "dog"}]
只需将过滤后数组的元素映射到它们的名称属性:
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));
或者将两者合二为一 reduce:
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)
创建一个空数组,使用 for
循环遍历现有 dogArray
,将名称推入新数组,然后 return 新数组。
const dogArray = animals.filter(function(animal) {
return animal.species === 'dog';
})
let dogNames = [];
for (let i in dogArray) {
dogNames.push(dogArray[i].name);
}
return dogNames;
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);
您可以将 属性 映射为 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);
您可以使用 .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);
我有一个对象:
const animals = [
{name: 'Fluffy', species: 'cat'},
{name: 'Crinkle', species: 'rabbit'},
{name: 'Wally', species: 'dog'},
{name: 'Roo', species: 'dog'},
{name: 'Felix', species: 'cat'},
]
我想使用高阶函数,例如 filter() 方法来获取动物对象数组,return 一个只包含所有狗的名字的数组,即 ["Wally", "Roo"]
。我现在的代码 return 是一个包含整个对象的数组,里面有狗。见下文:
const dogArray = animals.filter(function(animal) {
return animal.species === 'dog';
})
return dogArray;
// returns
// [{name: "Wally", species: "dog"},
// { name: "Roo", species: "dog"}]
只需将过滤后数组的元素映射到它们的名称属性:
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));
或者将两者合二为一 reduce:
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)
创建一个空数组,使用 for
循环遍历现有 dogArray
,将名称推入新数组,然后 return 新数组。
const dogArray = animals.filter(function(animal) {
return animal.species === 'dog';
})
let dogNames = [];
for (let i in dogArray) {
dogNames.push(dogArray[i].name);
}
return dogNames;
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);
您可以将 属性 映射为 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);
您可以使用 .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);