简体   繁体   English

将平面结构转换为对象

[英]Convert flat structure to object

In my attempts to make a generic CRUD system for Mongo DB and Mongoose I've ran into this challenge. 在为Mongo DB和Mongoose创建通用CRUD系统的尝试中,我遇到了这一挑战。 When the user updates a record and I read the req.body the fields are returned in a flat structure. 当用户更新记录并阅读req.body时,字段以平面结构返回。 But some of my models have nested records, hence the dot notation. 但是我的一些模型有嵌套记录,因此是点符号。

I need to unwrap below into an object so I can pass it to Mongo DB. 我需要将下面的对象解包,以便将其传递给Mongo DB。

I have: 我有:

var data = {
    'details.location.unit': undefined,
    'details.location.floor': undefined,
    'details.location.streetNumber': '67',
    'details.location.streetName': 'Brown St',
    'details.location.suburb': 'potts point',
    'details.location.postcode': 2011,
    'details.location.city': 'sydney',
    'details.location.state': 'nsw',
    'details.location.country': 'australia',
    'details.contact.phone': [ '(02) 2376 5678', '(02) 1234 5678' ],
    'details.contact.url': 'http://www.example.com',
    'details.contact.email': 'me@example.com'
}

And want to turn it into: 并希望将其转换为:

var data = {
    details:{
        location: {
            unit': undefined,
            floor': undefined,
            streetNumber': '67',
            streetName': 'Brown St',
            suburb': 'potts point'
        },

        contact: {
          phone': [ '(02) 2376 5678', '(02) 1234 5678' ],
          url: 'http://www.example.com',
          email: 'me@example.com',  
        }
    }
}

Notice the array in there as well. 注意那里的数组。 That needs to be JSON parsed. 那需要JSON解析。 Not quite sure how to attack this one! 不太清楚该如何进攻!

For another part of the project I used this function to access an object by string accessors. 对于项目的另一部分,我使用此函数通过字符串访问器访问对象。 Perhaps it can be repurposed? 也许可以重新利用它?

// @param {object} data
// @param {string} accessor e.g 'vehicles.cars.toyota'
// @return {*}
var getValueByAccessor = function (data, accessor) {

    var keys = accessor.split('.'),
        result = data;

    while (keys.length > 0) {
        var key = keys.shift();

        if (typeof result[key] !== 'undefined') {
            result = result[key];
        }

        else {
            result = null;
            break;
        }
    }

    return result;
}
var data = {
    'details.location.unit': undefined,
    'details.location.floor': undefined,
    'details.location.streetNumber': '67',
    'details.location.streetName': 'Brown St',
    'details.location.suburb': 'potts point',
    'details.location.postcode': 2011,
    'details.location.city': 'sydney',
    'details.location.state': 'nsw',
    'details.location.country': 'australia',
    'details.contact.phone': [ '(02) 2376 5678', '(02) 1234 5678' ],
    'details.contact.url': 'http://www.example.com',
    'details.contact.email': 'me@example.com'
}

var setNewValue = function (obj, chain, value) {
    var i = 0;
    while (i < chain.length - 1) {
        obj[chain[i]]=obj[chain[i]] || {};
        obj = obj[chain[i]];
        i++;
    }
    obj[chain[i]] = value;
};

var convertedObject={};
for(var a in data)if(data.hasOwnProperty(a)){
    var pathArr=a.split('.');
    var value=data[a];
    setNewValue (convertedObject, pathArr, value);
}

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

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