Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当连接在一起生成正确的JSON数据路径时,如何使用两个部分构建JSON对象_Javascript_Json - Fatal编程技术网

Javascript 当连接在一起生成正确的JSON数据路径时,如何使用两个部分构建JSON对象

Javascript 当连接在一起生成正确的JSON数据路径时,如何使用两个部分构建JSON对象,javascript,json,Javascript,Json,这是可能的,还是我该怎么做 arrayElement = new Object(); JSONkey = jsonData.table[0].key; // key in table[0] is "ident/Lesson/Value" JSONkey = JSONkey.replace(/\//g, '.'); // now JSONkey is "ident.Lesson.Value" arrayElement.JSONkey = "value1" // Can I do this or

这是可能的,还是我该怎么做

arrayElement = new Object();
JSONkey = jsonData.table[0].key; // key in table[0] is "ident/Lesson/Value"
JSONkey = JSONkey.replace(/\//g, '.'); // now JSONkey is "ident.Lesson.Value"

arrayElement.JSONkey = "value1" // Can I do this or how would I?
因此
arrayElement.JSONkey
arrayElement.ident.Lesson.Value

arrayElement=newobject();
arrayElement = new Object();
JSONkey = jsonData.table[0].key; // key in table[0] is "ident/Lesson/Value"
JSONkey = JSONkey.replace(/\//g, '.'); // now JSONkey is "ident.Lesson.Value"

deepRef(arrayElement, JSONkey, "value1");

function deepRef(ref, key, value) {
    var segments = key.split("."),
        n = segments.length;
    for (var i=0, skey; i<n; i++) {
        skey = segments[i];
        if (i < n - 1) {
            ref[skey] = {};
            ref = ref[skey];
        } else {
            ref[skey] = value;
        }
    }
}
JSONkey=jsonData.table[0].key;//表[0]中的键为“ident/Lesson/Value” JSONkey=JSONkey.replace(//\//g,'.');//现在JSONkey是“ident.Lesson.Value” deepRef(arrayElement,JSONkey,“value1”); 函数deepRef(ref、key、value){ 变量段=键。拆分(“.”), n=分段长度;
对于(var i=0,skey;我不清楚你在问什么。
jsonData
看起来像什么?你希望
arrayElement
在结尾看起来像什么?可能是@JamesThorpe的重复我认为这个问题是关于设置一个嵌套值,所以它不是真正的重复。你指出的问题是关于访问。@FaridNouriNeshat Fair够了。怎么样?是的,这是正确的,我想我应该提高我的搜索技能。我正在寻找:D这似乎很有效,谢谢。但是在我问题顶部列出的另一个链接中有一个更简化的版本。