javascript - 将平面结构转换为对象

标签 javascript node.js mongodb mongoose

在我尝试为 Mongo DB 和 Mongoose 制作通用 CRUD 系统时,我遇到了这个挑战。当用户更新记录并且我读取 req.body 时,字段将以平面结构返回。但我的一些模型具有嵌套记录,因此使用点表示法。

我需要将下面的内容展开为一个对象,以便我可以将其传递给 Mongo DB。

我有:

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 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',  
        }
    }
}

还要注意其中的数组。需要对 JSON 进行解析。不太确定如何攻击这个!

对于项目的另一部分,我使用此函数通过字符串访问器访问对象。也许它可以改变用途?

// @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);
}

关于javascript - 将平面结构转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32090700/

相关文章:

javascript - 有没有办法在课前或课后添加链接?

node.js - 来自 SOAPUI 的 SOAP 请求的 SOAP 信封丢失/无效

mongodb - 服务器选择在 10000 毫秒后超时 - 无法将 Compass 连接到本地主机上的 mongoDB

mongodb - 复制并重命名 MongoDB 中的文档字段

mongodb - Grails 4中mongo域类中的Autowire问题

javascript - 什么是未处理的 promise 拒绝?

Javascript 谷歌地图,按经度和纬度坐标显示图钉

javascript - 如何在 Angular <select>中获取选定的选项

mysql - 可能有 node.js redis 回退?

node.js - docker/bin/bash : nodemon: command not found