📜  如何从列表中跳过未定义的值并过滤反应 js 中的其余数据 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:10.995000             🧑  作者: Mango

代码示例2
let shops = [
  {
    name: 'shop1',
    tags: ['s', 'd', 'f'],
    country: 'rda'
  },
  {
    name: 'shop2',
    tags: ['e', 'd', 'r'],
    country: 'ke'
  },
  {
    name: 'shop3',
    tags: ['p', 'u', 'i'],
    country: 'rda'
  },
  {
    name: 'shop4',
    tags: ['a', 'k', 'l'],
    country: 'bu'
  }
]

function getRdaShops(){
  retun shops.map(sh=>{
    if(sh.name && sh.country==='rda') return sh.name
  }).filter(sh=>sh!==undefined)
}

//Answer: ['shop1', 'shop3']