gpt4 book ai didi

javascript - 如何在 Javascript 中更新嵌套 JSON 键?

转载 作者:行者123 更新时间:2023-12-03 00:33:48 24 4
gpt4 key购买 nike

我有一个嵌套的 JSON 结构,它始终有两个名为 top_nestmid_nest 的键。但是,这两个键将嵌套在什么级别,可能会根据数据集而有所不同。

top_nest 和 mid_nest 都有一个“备注”,最初始终为空。我想用数组中的值填充该 remark 键。

下面是我仅针对 mid_nest 键尝试过的代码:

var nestedjson = {
edition: '1.3.0',
OUTER_MOST: {
secondnest: {
mainstruct: {
top_nest: [
{
portblair: 'mtlb_wc_way0_fcl',
dir: 'left',
locs: ['/loc/local'],
remark: 'crc',
id: 1544593588899,
$count: 0,
$parent: 0,
$level: 1,
$height: 256,
},
],

mid_nest: [
{
ept: 'qop[3:0:0]',
remark: null,
race: 'false',
spt: ['mki[2:7:21]', 'epk[20:14.2:9.8]'],
inod: 'u_pqp',
mlace: 'pqp',
portblair: ['qq[31:9:24]', 'ax[2:16:1]'],
marcus: [
{
dir: 'left',
h_loc: ['/op/locs'],
sign: '0',
portblair_w: '81',
race_duty: '0',
},
{
race_duty: '0',
portblair_h: '28',
sign: '2',
dir: 'rigt',
h_loc: ['/pr/op'],
},
],
},
{
eptt: 'yie[3:0:0]',
remark: null,
race: 'false',
spt: ['mki[2:7:21]', 'epk[20:14.2:9.8]'],
inod: 'u_pqp',
mlace: 'pqp',
portblair: ['qq[31:9:24]', 'ax[2:16:1]'],
marcus: [
{
dir: 'left',
h_loc: ['/op/locs'],
sign: '0',
portblair_width: '8',
race_duty: '0',
},
{
race_duty: '0',
portblair_width: '8',
sign: '2',
dir: 'rigt',
h_loc: ['/pr/op'],
},
],
},
],
},
qq: 'ss',
},
},
dst: 'dss',
};

// function to take out the mid_nest structure to change its remark key
function findJson(json) {
var mid_ret = [];
for (var k in json) {
if (k === 'mid_nest') {
//console.log("breaking as mid reached");
mid_ret = json[k];
break;
} else if (typeof json[k] === 'object') {
findJson(json[k]);
}
}

console.log('--after break--');
return mid_ret;
}

var mid_ret = findJson(nestedJson);
var remark_arr = ['remark_1', 'remark2']; // array for assigning remarks

for (var i in remark_arr) {
var rem = remark_arr;
mid_ret[i]['remark'] = rem;
}

// assigning the changed mid_ret back to the original nestedJson
for (var k in nestedJson) {
if (k === 'mid_nest') {
nestedJson[k] = mid_ret;
} else if (typeof nestedJson[k] === 'object') {
continue;
}
}

但是,上面的代码无法像 findJson() 中那样工作,

(i) 即使在匹配 mid_nest 后,循环也不会中断,仍在迭代,因此不会返回正确的 mid_nest 值。

(ii) 因此,其余的处理(如通过数组值更改备注键并将其分配回原始结构)不起作用。

任何帮助将不胜感激。

谢谢。

最佳答案

尽管您需要重新遍历 JSON,但您的函数对我来说似乎过于复杂。看看这个工作示例。这应该简单地设置 remark值为 mid_nestmid_nest_remarkremark值为 top_nesttpo_nest_remark 。我会让你自己调整一下。

var nestedjson = {
edition: '1.3.0',
OUTER_MOST: {
secondnest: {
mainstruct: {
top_nest: [
{
portblair: 'mtlb_wc_way0_fcl',
dir: 'left',
locs: ['/loc/local'],
remark: 'crc',
id: 1544593588899,
$count: 0,
$parent: 0,
$level: 1,
$height: 256,
},
],

mid_nest: [
{
ept: 'qop[3:0:0]',
remark: null,
race: 'false',
spt: ['mki[2:7:21]', 'epk[20:14.2:9.8]'],
inod: 'u_pqp',
mlace: 'pqp',
portblair: ['qq[31:9:24]', 'ax[2:16:1]'],
marcus: [
{
dir: 'left',
h_loc: ['/op/locs'],
sign: '0',
portblair_w: '81',
race_duty: '0',
},
{
race_duty: '0',
portblair_h: '28',
sign: '2',
dir: 'rigt',
h_loc: ['/pr/op'],
},
],
},
{
eptt: 'yie[3:0:0]',
remark: null,
race: 'false',
spt: ['mki[2:7:21]', 'epk[20:14.2:9.8]'],
inod: 'u_pqp',
mlace: 'pqp',
portblair: ['qq[31:9:24]', 'ax[2:16:1]'],
marcus: [
{
dir: 'left',
h_loc: ['/op/locs'],
sign: '0',
portblair_width: '8',
race_duty: '0',
},
{
race_duty: '0',
portblair_width: '8',
sign: '2',
dir: 'rigt',
h_loc: ['/pr/op'],
},
],
},
],
},
qq: 'ss',
},
},
dst: 'dss',
};

var remark_arr = ['remark_1', 'remark2'];

$( document ).ready(function() {
var val = assignRemarks( nestedjson );
console.log(val)
});

function assignRemarks(theObject) {
var result = null;
if(theObject instanceof Array) {
for(var i = 0; i < theObject.length; i++) {
result = assignRemarks(theObject[i]);
if (result) {
break;
}
}
}
else
{
for(var prop in theObject) {
if(prop == 'top_nest') {
//set the remark property for top-nest here
theObject[prop].forEach(x => x.remark = 'top_nest_remark')
}
if(prop == 'mid_nest') {
for (let i = 0; i < theObject['mid_nest'].length; i++)
{
//set the remark property for mid-nest here
theObject['mid_nest'][i].remark = remark_arr[i];
}
}
if(theObject[prop] instanceof Object || theObject[prop] instanceof Array) {
result = assignRemarks(theObject[prop]);
}
}
}
return theObject;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

关于javascript - 如何在 Javascript 中更新嵌套 JSON 键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53738502/

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