我如何使用JavaScript重命名密钥

时间:2017-10-23 13:26:33

标签: javascript json node.js

let jsonObj = {
  "compCode": "0001",
  "vndrId": "0000000047",
  "vndrName": "NKJFKFJFJKJJ",
  "vndrName1": "jdkfjfkfjk",
  "shortName": "fjkfjkfjkf",
  "vndrStatus": "A",
  "vndrType": "R",
  "docStatus": "O",
  "createdOn": "1970-01-01T00:00:00.000+0000",
  "modifiedOn": "2017-10-11T10:33:35.000+0000",
  "lastUser": "ashok",
  "vndrAddr": [{
    "addrId": "1",
    "addr1": "jhghjbg",
    "addr2": "jgbfhhj",
    "addr3": "hjvfddfh",
    "city": "DJHHVH",
    "state": "JH",
    "pinCode": "855485",
    "effDate": "1970-01-01T00:00:00.000+0000",
    "effStatus": "A",
    "vndrContact": [{
        "contId": "1",
        "contName": "gnghh",
        "contDesg": "ghhgh",
        "contPh": "5625458",
        "contEmail": "gfhj@bjhg.com"
      }, {
        "contId": "1",
        "contName": "gnh",
        "contDesg": "ghgh",
        "contPh": "562558",
        "contEmail": "ghj@bjhg.com"
      }

    ]
  }, {
    "addrId": "2",
    "addr1": "jhghjbg",
    "addr2": "jgbfhhj",
    "addr3": "hjvfddfh",
    "city": "DJHHVH",
    "state": "JH",
    "pinCode": "855485",
    "effDate": "1970-01-01T00:00:00.000+0000",
    "effStatus": "A",
    "vndrContact": [{
      "contId": "3",
      "contName": "nghh",
      "contDesg": "hhgh",
      "contPh": "562558",
      "contEmail": "gfj@bhg.com"
    }, {
      "contId": "4",
      "contName": "gngh",
      "contDesg": "ghhh",
      "contPh": "56458",
      "contEmail": "gfh@bjh.com"
    }]
  }],
  "vndrRegn": [{
    "regnId": 1,
    "regnType": "V",
    "regnNo": "ABCDEFGHJ",
    "regnDate": "2016-10-01T00:00:00.000+0000",
    "regnAuth": "jfkjfjfjf",
    "regnExpiry": "2022-10-01T00:00:00.000+0000",
    "effDate": "2016-10-01T00:00:00.000+0000",
    "effStatus": "A"
  }, {
    "regnId": 2,
    "regnType": "S",
    "regnNo": "ABCDEFGHJ",
    "regnDate": "2016-10-01T00:00:00.000+0000",
    "regnAuth": "jfkjfjfjf",
    "regnExpiry": "2022-10-01T00:00:00.000+0000",
    "effDate": "2016-10-01T00:00:00.000+0000",
    "effStatus": "A"
  }]
}

我的对象看起来像这样。如何重命名数组或对象中的每个键并使用javascript创建键值数组?

我的结果应该是这样的

compCode:0001
vndrId:00088
vndrName:JXCHXDDJKCJ
vndrName1:JFVHSSSJFDH
shortName:jvgshqxz
vndrStatus:A
vndrType:R
docStatus:O
createdOn:18-10-2017 11:32:28
modifiedOn:23-10-2017 18:51:58
lastUser:ashok
vndrAddr[0].addrId:1
vndrAddr[0].addr1:vfdfvf
vndrAddr[0].addr2:nbnsdvd
vndrAddr[0].addr3:bdfb
vndrAddr[0].city:vbvfb
vndrAddr[0].state:JH
vndrAddr[0].pinCode:3332
vndrAddr[0].effDate:02-10-2012
vndrAddr[0].effStatus:A
vndrAddr[0].vndrContact[0].contId:1
vndrAddr[0].vndrContact[0].contName:jvffvjh
vndrAddr[0].vndrContact[0].contDesg:hvhjjvf
vndrAddr[0].vndrContact[0].contPh:vjhhjv
vndrAddr[0].vndrContact[0].contEmail:fhhf@fj.com
vndrAddr[1].addrId:2
vndrAddr[1].addr1:hjdfhjfhj
vndrAddr[1].addr2:vffvhjh
vndrAddr[1].addr3:hfvfhj
vndrAddr[1].city:hjvhjdf
vndrAddr[1].state:JH
vndrAddr[1].pinCode:255
vndrAddr[1].effDate:02-12-2012
vndrAddr[1].effStatus:A
vndrAddr[1].vndrContact[0].contId:1
vndrAddr[1].vndrContact[0].contName:dfvhjf
vndrAddr[1].vndrContact[0].contDesg:fvjhfvhj
vndrAddr[1].vndrContact[0].contPh:fvhjjhfv
vndrAddr[1].vndrContact[0].contEmail:hdhd@hf.com

1 个答案:

答案 0 :(得分:0)

您可以通过使用括号保存数组索引的类型来采用迭代和递归方法。稍后为该值指定路径的新属性。



function setPath(object) {

    function iter(object, path, isArray) {
        Object.keys(object).forEach(function (key) {
            var temp = path + (isArray ? '[' + key + ']' : (path && '.') + key);
            if (object[key] && typeof object[key] === 'object') {
                return iter(object[key], temp, Array.isArray(object[key]));
            }
            result[temp] = object[key];
        });
    }

    var result = {};
    iter(object, '');
    return result;
}

var object = { a: { b: [{ c: { d: 'foo' } }, 'bar'] } };

console.log(setPath(object));