?
code:
const temp = this.props.app && this.props.app.byId && Object.keys(this.props.app.byId)[0] ? Object.keys(this.props.app.byId)[0] : undefined;
structure of app:
app: {
byId: {
url1: {
name: 'xyz',
address: "Text"
},
url2: {
name: 'xyz',
address: "Text"
}
}
}
byid is object not array lel
I want temp to have value url1 so thats why I have used Object.keys(this.props.app.byId)[0]
so how can I fix this issue?
Object.keys(this.props.app.byId)[0] is this wrong?
any idea what I have done wrong?
which property do you want to read?
this.props.app?.byId?.url1 ?? default-value
what is default value
Whatever you want. It was just a placeholder
but I don't know the content inside byId it can be url1 or url2 or anything so I just want to extract first key and set it to variable how can I do that case?
Important: It will work only on NodeJS version > 14.5
any idea how can I do that?
let me explain problem url1 and url2 will not be there initially so byId is empty object now once that object is populated I want to set 1st key which is inside byId to a variable
If it doesn't exist?
who even designs such API ? like you are trying to access [0]th property and object keys are interchangeable
Object.keys returns array
For the new features ?
Nullish Coalescing and Optional Chaining
No I meant he wanted to access [0] object key order can vary He wouldn't get what he is expecting
no idea backend dev's did that I actually solved the problem by doing this: if(this.props.app && this.props.app.byId && Object.keys(this.props.byId).length > 0) { }
Did you miss .app property ?
Обсуждают сегодня