Javascript 从数组中的多个对象筛选属性

Javascript 从数组中的多个对象筛选属性,javascript,ecmascript-6,ecmascript-5,Javascript,Ecmascript 6,Ecmascript 5,我有一个像这样的对象数组 const data = [ {id: 1, locale: 'en'}, {id: 2, locale: 'nl'} ] 现在,我正在尝试过滤掉数组中每个项目的locale属性(不是永久删除它,只需过滤一次),因此我的数据理想情况下类似于: const data = [ {id: 1}, {id: 2} ] 我试过了 使用map函数来展开属性,但我被困在如何继续这样做的问题上 this.translations.map(trans

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

const data = [
    {id: 1, locale: 'en'},
    {id: 2, locale: 'nl'}
]
现在,我正在尝试过滤掉数组中每个项目的locale属性(不是永久删除它,只需过滤一次),因此我的数据理想情况下类似于:

const data = [
    {id: 1},
    {id: 2}
]
我试过了
  • 使用map函数来展开属性,但我被困在如何继续这样做的问题上

    this.translations.map(translation => {
        return { ...translation }
    })
    
您可以使用来提取
语言环境
,并保留其他语言环境:

const数据=[
{id:1,区域设置:'en'},
{id:2,区域设置:'nl'}
]
const withoutLocale=data.map(({locale,…rest})=>rest)
log(不带区域设置)
使用返回映射像这样执行
({key:value})

针对您的案例

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

this.translations.map(translation => {
    return ({['id']:translation.id })
})
const data=[{id:1,区域设置:'en'},{id:2,区域设置:'nl'}];
var res=data.map(a=>({['id']:a.id}));

console.log(res)
以下是使用
map()
reduce()
filter()。此方法适用于要过滤掉的动态关键帧

const数据=[
{id:1,区域设置:'en'},
{id:2,区域设置:'nl'}
]
let filter=['locale']
功能移除键(键,arr){
返回data.map(x=>Object.keys(x).filter(b=>!keys.includes(b)).reduce((ac,a)=>({…ac[a]:x[a]}),{}))
}

日志(移除键(过滤器、数据))这是一个完美的解决方案+1实际上,我想排除其他值。因为可能会有更多的价值