0

I have an object in Javascript as follows:

profile= {
                profilePicture: null,
                username: null,
                age: null,
                gender: null,
                followedBy: [],
                following: [],
                aboutMe: null
            }

And I would like to destructure all of the properties into variables, like such:

const {userName, age, gender, followedBy, following, aboutMe} = profile
    

Let's say in the future this profile object will have 30 properties, how can I iterate over all the properties instead of typing them one by one? I was thinking something like this

const {userName, age, ...rest} = profile

but instead of the rest variable, it actually gives me all the other property names as stand-alone variable names that I can use. I tried something like

const {...Object.keys(profile)} = profile

but it's not working.

Any ideas?

Iggee Xy
  • 83
  • 1
  • 8

1 Answers1

0

Simply change the curly bracket to square bracket

For Example

const [userName, age, gender, followedBy, following, aboutMe] = profile

const [userName, age, ...rest] = profile
Maxwell D. Dorliea
  • 1,086
  • 1
  • 8
  • 20