javascript string to variable

后端 未结 5 1647
星月不相逢
星月不相逢 2020-12-21 04:26

I am receiving a JSON string from an ajax call and would like to convert a value to a predefined variable:

var predefined = \"hello world\";
var foo = {\"msg         


        
5条回答
  •  伪装坚强ぢ
    2020-12-21 05:01

    var out = eval(foo.msg); // out is now "hello world"
    

    Note: do not use eval() if you are not sure what the content of foo.msg is.

    or

    var out = foo.msg=="predefined" ? predefined : foo.msg;
    

提交回复
热议问题