根据属性返回对象值

时间:2019-02-16 05:48:56

标签: javascript arrays javascript-objects

我有一个包含多种动物的物体:

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()方法由于某种原因而无效。

谢谢。

5 个答案:

答案 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)

您还可以使用filtermap

通过过滤器,我们得到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

相关问题