我有一个包含多种动物的物体:
var toonimals = [ {name: 'Itchy', animal: 'mouse'}, {name: 'Stimpy', animal: 'cat'}, {name: 'Daffy', animal: 'duck'}, {name: 'Scratchy', animal: 'cat'}, {name: 'Ren', animal: 'dog'}, {name: 'Felix', animal: 'cat'}]
我只想返回只猫的动物的名字。我正在努力做到这一点。这是我的尝试:
var cats = []
function onlyCats(array) {
if (toonimals.animal === 'cat') {
cats.push(toonimals.name)
}
return cats
}
console.log(onlyCats(toonimals));
当前,它仅返回空数组,因此.push()
方法由于某种原因而无效。
谢谢。
答案 0 :(得分:1)
您实际上需要遍历toonimals
数组。您可以使用.filter
和.map
来实现这一目的:
var toonimals = [ {name: 'Itchy', animal: 'mouse'}, {name: 'Stimpy', animal: 'cat'}, {name: 'Daffy', animal: 'duck'}, {name: 'Scratchy', animal: 'cat'}, {name: 'Ren', animal: 'dog'}, {name: 'Felix', animal: 'cat'}]
const onlyCats = array => array
.filter(({ animal }) => animal === 'cat')
.map(({ name }) => name);
console.log(onlyCats(toonimals));
或者,仅重复一次 ,请使用reduce
:
var toonimals = [ {name: 'Itchy', animal: 'mouse'}, {name: 'Stimpy', animal: 'cat'}, {name: 'Daffy', animal: 'duck'}, {name: 'Scratchy', animal: 'cat'}, {name: 'Ren', animal: 'dog'}, {name: 'Felix', animal: 'cat'}]
const onlyCats = array => array
.reduce((a, { name, animal }) => {
if (animal === 'cat') {
a.push(name);
}
return a;
}, []);
console.log(onlyCats(toonimals));
答案 1 :(得分:1)
您可以使用forEach
var toonimals = [ {name: 'Itchy', animal: 'mouse'}, {name: 'Stimpy', animal: 'cat'}, {name: 'Daffy', animal: 'duck'}, {name: 'Scratchy', animal: 'cat'}, {name: 'Ren', animal: 'dog'}, {name: 'Felix', animal: 'cat'}]
let op = []
toonimals.forEach(({name,animal})=>{
if(animal === 'cat'){
op.push(name)
}
})
console.log(op)
通过过滤器,我们得到animal = cat
的对象,然后我们映射每个过滤后的元素的名称。
var toonimals = [ {name: 'Itchy', animal: 'mouse'}, {name: 'Stimpy', animal: 'cat'}, {name: 'Daffy', animal: 'duck'}, {name: 'Scratchy', animal: 'cat'}, {name: 'Ren', animal: 'dog'}, {name: 'Felix', animal: 'cat'}]
let cats = toonimals.filter(({animal})=> animal ==='cat').map(({name})=>name)
console.log(cats)
答案 2 :(得分:1)
您必须遍历数组以filter()
动物。然后使用map()
修改数组以返回名称:
var toonimals = [ {name: 'Itchy', animal: 'mouse'}, {name: 'Stimpy', animal: 'cat'}, {name: 'Daffy', animal: 'duck'}, {name: 'Scratchy', animal: 'cat'}, {name: 'Ren', animal: 'dog'}, {name: 'Felix', animal: 'cat'}];
function onlyCats(array) {
return array.filter(a => a.animal === 'cat').map(a => a.name);
}
console.log(onlyCats(toonimals));
在forEach()
的帮助下:
var toonimals = [ {name: 'Itchy', animal: 'mouse'}, {name: 'Stimpy', animal: 'cat'}, {name: 'Daffy', animal: 'duck'}, {name: 'Scratchy', animal: 'cat'}, {name: 'Ren', animal: 'dog'}, {name: 'Felix', animal: 'cat'}];
var cats = []
function onlyCats(array) {
array.forEach(function(animal){
if (animal.animal === 'cat') {
cats.push(animal.name)
}
});
return cats;
}
console.log(onlyCats(toonimals));
答案 3 :(得分:0)
使用Array.filter
过滤具有name === 'cat'
和Array.from
的对象以转换结果并从过滤后的数组中获取新数组。
const toonimals = [ {name: 'Itchy', animal: 'mouse'}, {name: 'Stimpy', animal: 'cat'}, {name: 'Daffy', animal: 'duck'}, {name: 'Scratchy', animal: 'cat'}, {name: 'Ren', animal: 'dog'}, {name: 'Felix', animal: 'cat'}];
let catOnly = Array.from(toonimals.filter(obj => obj.animal === 'cat'), animal => animal.name);
console.log(catOnly);
答案 4 :(得分:0)
您可以只使用一个map
,然后用filter
的{{1}}删除Boolean
:
undefined