按键查找并按嵌套json对象中的值替换 [英] Find by key and replace by value in nested json object

查看:128
本文介绍了按键查找并按嵌套json对象中的值替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json对象,它可以嵌套,还有另一个包含键/值对的对象.我想通过将两个对象的键都匹配来将第一个对象的值替换为第一个对象.

I have an json object It can be nested and I have an second object containing key/value pair. I want to replace the second object's value by first one by matching key both object's key.

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中获得解决方案,在我尝试处理的快速应用程序中,它给了我一个空对象.

I have tried many ways [http://jsfiddle.net/FM3qu/7/][1] this way i'm able to get solution in Jsfiddle, In my express application when I try to process it gives me an empty object.

我想要这样的东西

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

谢谢

推荐答案

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

You could first save all references and then assign the data, you have.

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);

这篇关于按键查找并按嵌套json对象中的值替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆