如何从对象中提取值并将其放入数组

时间:2020-09-21 06:41:28

标签: javascript arrays filter angular8 immutability

我想提取filterPillvalue并将其放入一个数组中,该数组的输出应如下所示:

mode = ['anyValue','Repsonding','Unresponsive']

这是我的尝试

this.items = [
  {
    filterPillValue: 'anyValue',
    id: 'all-systems',
    label: 'All systems'
  },
  {
    filterPillValue: 'Responding',
    id: 'responding',
    label: 'Responding systems'
  },
  {
    filterPillValue: 'Unresponsive',
    id: 'unresponsive',
    label: 'Unresponsive systems'
  }
];

我的尝试在这种情况下不起作用

mode = this.items.filter(x=>x.filterPillValue);

2 个答案:

答案 0 :(得分:2)

这将起作用。

mode = this.items.map(({filterPillValue}) => filterPillValue);

mode = this.items.map((item) => item.filterPillValue);

答案 1 :(得分:2)

items = [
  {
    filterPillValue: 'anyValue',
    id: 'all-systems',
    label: 'All systems'
  },
  {
    filterPillValue: 'Responding',
    id: 'responding',
    label: 'Responding systems'
  },
  {
    filterPillValue: 'Unresponsive',
    id: 'unresponsive',
    label: 'Unresponsive systems'
  }
];

const node=items.map(item=>item.filterPillValue)
console.log(node)