【问题标题】:Find by key and replace by value in nested json object在嵌套的 json 对象中按键查找并按值替换
【发布时间】:2016-06-24 12:03:31
【问题描述】:

我有一个 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"....

谢谢

【问题讨论】:

    标签: javascript node.js express ecmascript-6


    【解决方案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);

    【讨论】:

    • 你没有看到上面的结果?
    • 是的,我明白了,让我编辑问题,当我使用 TypeError: Cannot set property 'DealerRef' of undefined 运行上述代码错误时,我遇到了我的 api 问题
    • 不知道原始对象,我只能猜测。
    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2021-08-16
      • 2019-03-02
      • 2021-10-04
      • 2018-01-02
      • 2019-07-18
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      • 2016-01-08
      相关资源
      最近更新 更多