Reputation: 245
I have an object that includes a variety of animals:
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'}]
I'm wanting to return the names of the animals that are cats, only. I'm struggling to do so. Here is my attempt:
var cats = []
function onlyCats(array) {
if (toonimals.animal === 'cat') {
cats.push(toonimals.name)
}
return cats
}
console.log(onlyCats(toonimals));
Currently, it is only returning the empty array so the .push()
method isn't working for some reason.
Thank you in advance.
Upvotes: 2
Views: 103
Reputation: 16908
Use Array.filter
to filter objects having name === 'cat'
and Array.from
to transform the result and get a new array from the filtered array.
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);
Upvotes: 0
Reputation: 44145
You can just use one map
, then filter
by Boolean
to remove undefined
:
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 catNames = arr => arr.map(e => { if (e.animal == "cat") return e.name }).filter(Boolean);
console.log(catNames(toonimals));
Upvotes: 0
Reputation: 68933
You have to loop through the array to filter()
the animal. Then use map()
to modify the array to return the name:
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));
In your way with the help of 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));
Upvotes: 1
Reputation: 37775
You can use 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)
You can also use filter and map.
by filter we get object with animal = cat
and than we map name of each filtered element.
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)
Upvotes: 1
Reputation: 371168
You need to actually iterate over the toonimals
array. You can achieve this concisely with .filter
and .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));
Or, to only iterate once, use 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));
Upvotes: 1