0

Sample properties file.

const properties = {
    subproper: {
        name:'denise',
        age:'64'
     }
}

Accessing this in code base

subproper.name or subproper.age

What if I want to use dynamic keys?

eg: I tried these two, but both give the error

{subproper.$[`${this.state.propertie1}`] // $ is not a function
{subproper.$`${this.state.propertie1}`} //cannot call value1 of undefined (value1 is value of key this.state.propertie1)

3 Answers 3

2

Try accessing keys using [] notation.

const props = {name:"Jon",surname:"Doe"}

const dynamicKey1 = "name";
const dynamicKey2 = "surname"

console.log(props[dynamicKey1]);
console.log(props[dynamicKey2]);
Sign up to request clarification or add additional context in comments.

Comments

2

You can do something like this, replace dynamic with your property.

const properties = { subproper: { name:'denise', age:'64' } }

const dynamic = "name";

console.log(properties.subproper[dynamic])

Comments

0

Simply use [ ] or use Object.values(Objectname)

example:{subproper.[Objectname]} or {subproper.[Object.values(objectname)[array_index]]}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.