Rashid Khan
Rashid Khan

Reputation: 451

Using variable as object names in react native

I am trying to use a dynamic name for a property of an object.

For example, in the function below, I want to use the value of variable 'catagoryId' to get the child that results into the value for the variable catagoryId. For example if catagoryId = book. I want the last line to resolve to "endAtObject.orderValues.book".

        export const feedFetch = (catagoryId, endAtObject) => {
        endAtObject.orderValues.<<<catagoryId>>> 
        }

How do I do this? Any help will be highly appreciated!

Upvotes: 2

Views: 2064

Answers (1)

g2jose
g2jose

Reputation: 1465

What you're looking for is 'computed property names'. This will do the job.

export const feedFetch = (catagoryId, endAtObject) => {
    endAtObject.orderValues[catagoryId]
}

You can find more info at https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Object_initializer (CMD+F "Computed property names")

Upvotes: 3

Related Questions