gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:33:45 25 4
gpt4 key购买 nike

这是否可能,或者我该怎么做?

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.JSONkeyarrayElement.ident.Lesson.Value 相同

最佳答案

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;
}
}
}

关于javascript - 当连接在一起产生正确的 JSON 数据路径时,如何使用两个部分构建我的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28157941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com