将对象的所有键转换为 ReactJS 中的变量
convert all keys of object to variable in ReactJS
在 React 中你可以做类似的事情
const obj = {
thing: 'a',
}
那么 const { thing } = obj
是 shorthand for const thing = obj.thing
有没有办法一次转换所有对象 key/value 对?
Object.keys(obj).map((key) => {
const eval(key) = obj[key];
}
类似的东西。
提前致谢!
唯一的方法是解构对象,但是您需要知道对象的属性:
const { prop1, prop2, prop3 } = obj;
顺便说一下,您使用“.map”的示例将无法工作,因为您正在创建一个本地常量,该常量在该函数之外无法访问。
在 React 中你可以做类似的事情
const obj = {
thing: 'a',
}
那么 const { thing } = obj
是 shorthand for const thing = obj.thing
有没有办法一次转换所有对象 key/value 对?
Object.keys(obj).map((key) => {
const eval(key) = obj[key];
}
类似的东西。
提前致谢!
唯一的方法是解构对象,但是您需要知道对象的属性:
const { prop1, prop2, prop3 } = obj;
顺便说一下,您使用“.map”的示例将无法工作,因为您正在创建一个本地常量,该常量在该函数之外无法访问。