最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

NodeJS将路径数组转换为JSON

网站源码admin20浏览0评论

NodeJS将路径数组转换为JSON

NodeJS将路径数组转换为JSON

是否有一种简单的方法来转换具有路径=>值对(例如)的对象列表]

path: /wip/bish/name // value: bing
path: /wip/bash/name // value: bang
path: /wip/bosh/name // value: bong   

所以我最终得到一个像这样的json对象:

{
   "wip": {
       "bish": {
           "name": "bing"
       },
       "bash": {
           "name": "bang"
       },
       "bosh: {
           "name": "bong"
       }
   }
}

源数据看起来像:

{ Parameters:
    [ 
        { 
            Name: '/wip/bish/name',            
            Value: 'bing' 
        },
        { 
            Name: '/wip/bash/name',
            Value: 'bang'
        },
        { 
            Name: '/wip/bosh/name',            
            Value: 'bong'
        }
    ] 
}
回答如下:

原来有一个名为'flat'的软件包,带有一个'unflatten'函数,可以解决我的问题。

https://www.npmjs/package/flat

var unflatten = require('flat').unflatten

unflatten({
    'three.levels.deep': 42,
    'three.levels': {
        nested: true
    }
})

// {
//     three: {
//         levels: {
//             deep: 42,
//             nested: true
//         }
//     }
// }

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论