簡體   English   中英

根據屬性返回對象值

[英]Returning Object Value Based On Property

我有一個包含多種動物的物體:

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()方法由於某種原因無法正常工作。

先感謝您。

您實際上需要遍歷toonimals數組。 您可以使用.filter.map簡潔地實現此.filter

 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)); 

您可以使用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) 

您必須遍歷數組以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)); 

使用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); 

您可以只使用一張map ,然后按Boolean filter以刪除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)); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM