constructor() { super() this.state = { privilegesOption: privilegesOption: [{ key: 1, text: "Admin", value: 1 }, { key: 2, text: "Cashier", value: 2 }] } this.foo = this.foo.bind(this) } foo() { let privilegesOut = this.state.privilegesOption.find(e => e.value === 1); console.log(privilegesOut.text) }
Why i keep get this error if i call object property after using.find? if i use this.state.privilegesOption[0].text, its worked
A few erros with this which is best answered with some space, I think what you were trying to do was this, I have turned privilegesOption
into an array []
containing objects {}
where the object key:value pairs are seperated by a comma ,
this.state = {
privilegesOption: [
{
key: 1,
text: "Admin",
value: 1
},
{
key: 2,
text: "Cashier",
value: 2
}
]
}
And to find
the object where value is 1 you were completely right
let privilagesOut = this.state.privilegesOption.find(x => x.value == 1)
Your posted code snippet has a syntax error (press "Run code snippet" to see it). Here is a complete code snippet with fixed syntax errors:
class Y {} class X extends Y { constructor() { super(); this.state = { privilegesOption: [ { key: 1, text: "Admin", value: 1 }, { key: 2, text: "Cashier", value: 2 } ] }; let privilegesOut = this.state.privilegesOption.find(e => e.value === 1); console.log(privilegesOut.text); } } new X();
It successfully outputs "Admin" on the console.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.