gpt4 book ai didi

javascript - 遍历嵌套的 JSON 树并更改值

转载 作者:行者123 更新时间:2023-11-29 19:38:47 26 4
gpt4 key购买 nike

我有一个具有以下结构的 JSON 树:

{
"projects": {
"Proj1": {
"milestones": {
"default": "20150101",
"default2": "20140406",
"default3": "20140101",
"default4": "20131231",
"default5": "20131220"
}
},
"Proj2": {
"milestones": {
"default": "20131231",
"default2": "20131220"
}
}
}
}

我有将其读入网页的代码,文本中包含“默认”部分,文本框/表单中包含数字/日期。这个想法是您可以更改日期并提交,这会转到后端并写入文件。所有这些在大多数情况下都有效。我能弄清楚的是如何遍历我拥有的 JSON 树并写入新值。例如:

访问 JSONTREE.projects.Proj1.milestones.default 返回该键的值。使用该调用设置值会适本地更改值。我想要做的是遍历整个树并根据表单框中的内容设置“默认值”的值。我有这个:

$.each(formJSON.projects, function (projectName) {
$.each(this, function (selection) {
$.each(this, function (milestones, date) {
var saltKey = projectName + "-" + milestones;
date = document.getElementById(saltKey).value;
});
});
});

但它什么也不做,即使“alert(date)”返回一个值。我怀疑这是因为它是值,而不是对对象的引用,但是我怎样才能得到对象呢?我怀疑这很简单,但我不是 jQuery/JS 专家。

TL;DR 如何获取对嵌套 JSON 树中键的引用,以便我可以更改值?

编辑:好的,我想这就是我需要的:JS/Jquery - using variable in json selector .我将“日期”部分更改为: formJSON.projects[projectName][selection][milestones] = document.getElementById(saltKey).value;这似乎有效。

最佳答案

我遇到了同样的问题,这是我迭代 JSON 对象 (MongoDB) 并修改某些元素的解决方案,无论这些元素是对象的属性还是属性是包含更多对象的数组。我知道这个问题很老,但它对某人有用。假设我们有一个像这样甚至更复杂的对象。

var mixData = {
"Hello": "World",
"Foo" : "Bar",
"sudo": [
{ "apt-get": "upgrade" , "force": false },
{ "apt-get": "update" , "force": true}
],
"colors": ["blue","green"],
"numbers":{
"integer": [
{"num": 1},
{"num": 2},
{"num": 3}
],
"operator": "addition"
}
};

我们想替换一些字符串(在本例中为蓝色),但我们不知道我们的数据在哪里或什么样的构造函数。

// Detect if the element is an Array
function isElementArray(element){
return (element.constructor === Array ? true : false);
}
//Detect if the element is an Object
function isElementObject(element){
return (element.constructor === Object ? true : false);
}

现在我们有了这两个函数,我们使用第三个函数来迭代项目,无论是对象还是数组。

function iterate(element){
//Check if the element is an Object
if(isElementObject(element)){
for (var property in element){
if(isElementObject(element[property])){
element[property] = iterate(element[property]);
}else if(isElementArray(element[property])){
//An array
for(var x = 0; x < element[property].length; x++){
element[property][x] = iterate(element[property][x]);
}
}else{
if(element.hasOwnProperty(property)){
console.log("New object inside object property");
element[property] = iterate(element[property]);
}else{
element[property] = replaceElement(element[property].toString());
console.log("Single Element: " + element[property] )
console.log(element + " " + element[property]);
}
}
}
}else if(isElementArray(element)){
//An Array
for (var x = 0; x < element.length; x++){
element[x] = iterate(element[x]);
}
}else{
//Single element in array or property
element = replaceElement(element.toString());
console.log("Single Element : " + element);
}
return element;
}

以及我们需要用来替换字符串的函数。

function replaceElement(element){
if(element === "blue"){
return "Blue is the warmest color"
}else{
return element;
}
}

最后,我们可以得到结果:

console.log(iterate(mixData));

结果是:

{
"Hello": "World",
"Foo" : "Bar",
"sudo": [
{ "apt-get": "upgrade" , "force": false },
{ "apt-get": "update" , "force": true}
],
"colors": ["Blue is the warmest color","green"],
"numbers":{
"integer": [
{"num": 1},
{"num": 2},
{"num": 3}
],
"operator": "addition"
}
};

您可以更改替换功能以满足您的需要。当然,删除所有控制台日志。

关于javascript - 遍历嵌套的 JSON 树并更改值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24250488/

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