Search code examples
javascriptecmascript-6ecmascript-5

Filter property from multiple objects in array


I have an array of objects that look like this

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

Now I'm trying to filter out the locale property in every item in the array (not delete it for good, just filter it out this once), so my data would ideally resemble this:

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

I've tried

  • Using a map function to spread out the properties, but I'm stuck as to how to continue with this.

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

Solution

  • You can use parameter destructuring to extract the locale and keep the other ones:

    const data = [
        {id: 1, locale: 'en'},
        {id: 2, locale: 'nl'}
    ]
    
    const withoutLocale = data.map(({locale, ...rest}) => rest)
    
    console.log(withoutLocale)