可能重复:
是否使用var在密钥中创建JSON对象?
我想通过使用(String)变量的值作为键来在JavaScript中构造JSON对象.但我得到的是变量的名称作为关键.
example.js:
function constructJson(jsonKey, jsonValue){
var jsonObj = { "key1":jsonValue, jsonKey:2};
return jsonObj;
}
Run Code Online (Sandbox Code Playgroud)
电话
constructJson("key2",8);
Run Code Online (Sandbox Code Playgroud)
返回一个JSON - > {"key1":8,"jsonKey":2}但我希望{"key1":8,"key2":2}.
有谁知道如何实现这一目标?似乎是一个简单的问题,但我找不到解决方案
提前,罗尼
Ant*_*zov 42
function constructJson(jsonKey, jsonValue){
var jsonObj = {"key1": jsonValue};
jsonObj[jsonKey] = "2";
return jsonObj;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
38839 次 |
| 最近记录: |