从数组中的多个对象过滤属性

时间:2019-03-12 14:23:26

标签: javascript ecmascript-6 ecmascript-5

我有一个看起来像这样的对象数组

const data = [
    {id: 1, locale: 'en'},
    {id: 2, locale: 'nl'}
]

现在,我正在尝试过滤掉数组中每个项目中的语言环境属性(不要永久删除它,只需将其过滤掉一次),因此我的数据在理想情况下类似于:

const data = [
    {id: 1},
    {id: 2}
]

我尝试过

  • 使用地图函数分散属性,但是我对如何继续执行此操作感到困惑。

    this.translations.map(translation => {
        return { ...translation }
    })
    

3 个答案:

答案 0 :(得分:10)

您可以使用parameter destructuring提取locale并保留其他内容:

const data = [
    {id: 1, locale: 'en'},
    {id: 2, locale: 'nl'}
]

const withoutLocale = data.map(({locale, ...rest}) => rest)

console.log(withoutLocale)

答案 1 :(得分:0)

像这样print(soup.find("h3", string="Description").find_next_sibling().text) 并附有返回图

针对您的情况

({key:value})

工作示例

this.translations.map(translation => {
    return ({['id']:translation.id })
})

答案 2 :(得分:0)

这是使用(df.set_index('Code').astype(str)+',').sum(level=0).apply(lambda x : x.str.strip(',')) Out[50]: Name X Y Code 123 A,C 10,15 11,16 456 B 12 13 map() reduce()的方式。此方法用于滤除动态键。

filter()