ReactJS如何使用另一个变量引用对象

时间:2020-11-03 16:16:01

标签: reactjs

我有一个包含字符串的变量。该字符串本身就是另一个对象的名称。

const myObject = {"some": "Object in JSON format"};

const myObjectName = 'myObject';

如何使用变量'myObjectName'中包含的字符串来调用操作myObject的函数?

1 个答案:

答案 0 :(得分:2)

const myObject = {"some": "Object in JSON format"};
const myObjectName = 'myObject';
function myFunction (obj) {
   console.log(obj);
}

eval(`myFunction(${myObjectName})`);

This works but eval is dangerous and should be avoided