按键查找并在嵌套的json对象

时间:2016-06-24 12:03:31

标签: javascript node.js express ecmascript-6

我有一个json对象它可以嵌套,我有第二个包含键/值对的对象。我希望通过匹配键对象的键来替换第二个对象的值。

let firstObj = {
        "enquiry": {
            "Lead": {
                "SubLead": {
                    "DealerRef": "test",
                    "DealerFloor": "test",
                    "Region": "test",
                    "Source": {
                        "Special": "test",
                        "TestDrive": "test",
                        "TradeIn": "test",
                        "Finance": "test"
                    }
                },
                "Contact": {
                    "Info": {
                        "FirstName": "test",
                        "Surname": "test",
                        "Email": "test",
                        "OfficePhone": "test",
                        "CellPhone": "test"
                    }
                },
                "Seeks": {
                    "Stock": {
                        "Used": "test",
                        "Brand": "test",
                        "Model": "test",
                        "StockNr": "test"
                    }
                }
            }
        }
    }

我的阵列

let secondObj = {
  DealerRef: '18M',
  DealerFloor: 'UCP',
  Region: 'Western Cape',
  FirstName: 'abc',
  Surname: 'xyz',
  Email: 'test@ctm.co.za',
  OfficePhone: '2343243',
  CellPhone: '2343243',
  Used: '1',
  Brand: 'QAE',
  Model: 'test',
  StockNr: 'SEDONA',
  Special: '2013 Kia Sedona',
  TestDrive: '0',
  TradeIn: '0',
  Finance: '0' 
};

我尝试了很多方法[http://jsfiddle.net/FM3qu/7/][1]这样我就可以在Jsfiddle中获得解决方案了。在我的快速应用程序中,当我尝试处理它时,它给了我一个空的对象。

我想要这样的东西

"enquiry": {
  "Lead": {
      "SubLead": {
          "DealerRef": "18M",
          "DealerFloor": "UCP",
          "Region": "Western Cape"....

谢谢

2 个答案:

答案 0 :(得分:2)

您可以遍历firstObj并使用secondObj

替换键/值
function iterateObj(obj){
  for(var key in obj){
    if(obj.hasOwnProperty(key)){
      if(typeof obj[key] === 'object'){
        iterateObj(obj[key]);
      }
      else if(secondObj[key]!=undefined){
        obj[key] = secondObj[key]
      }
    }
  }
}
iterateObj(firstObj)

console.log(firstObj); // this will give proper results

答案 1 :(得分:1)

您可以先保存所有引用,然后分配数据。



function update(object, data) {
    function getAllKeys(o) {
        Object.keys(o).forEach(function (k) {
            if (typeof o[k] === 'object') {
                return getAllKeys(o[k]);
            }
            keys[k] = o;
        });
    }

    var keys = Object.create(null);

    getAllKeys(object);
    Object.keys(data).forEach(function (k) {
        if (keys[k] && k in keys[k]) { // check if key for update exist
            keys[k][k] = data[k];
        }
    });
}

var object = { "enquiry": { "Lead": { "SubLead": { "DealerRef": "test", "DealerFloor": "test", "Region": "test", "Source": { "Special": "test", "TestDrive": "test", "TradeIn": "test", "Finance": "test" } }, "Contact": { "Info": { "FirstName": "test", "Surname": "test", "Email": "test", "OfficePhone": "test", "CellPhone": "test" } }, "Seeks": { "Stock": { "Used": "test", "Brand": "test", "Model": "test", "StockNr": "test" } } } } },
    data = { DrNo: 666, DealerRef: '18M', DealerFloor: 'UCP', Region: 'Western Cape', FirstName: 'abc', Surname: 'xyz', Email: 'test@ctm.co.za', OfficePhone: '2343243', CellPhone: '2343243', Used: '1', Brand: 'QAE', Model: 'test', StockNr: 'SEDONA', Special: '2013 Kia Sedona', TestDrive: '0', TradeIn: '0', Finance: '0' };

update(object, data);

console.log(object);