繁体   English   中英

如何在Javascript中更新嵌套的JSON密钥?

[英]How to update a nested JSON key in Javascript?

我有一个嵌套的JSON结构,它将始终具有两个键,名称分别为top_nestmid_nest 但是,这两个键将嵌套在什么级别上,具体取决于数据集。

top_nest和mid_nest中的每个都有一个“备注”,该备注最初始终为null。 我想用数组中的值填充该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重新进行处理,但您的函数对我而言似乎过于复杂。 看一下这个工作示例。 这应该简单地将mid_nestremark值设置为mid_nestmid_nest_remarkremarktop_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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM