Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 }
    })
    
like image 319
Miguel Stevens Avatar asked Apr 09 '26 17:04

Miguel Stevens


1 Answers

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)
like image 166
Daniel Ramos Avatar answered Apr 11 '26 08:04

Daniel Ramos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!