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

javascript - How to convert an array of paths into JSON structure? - Stack Overflow

programmeradmin0浏览0评论

I found the question How to convert a file path into treeview?, but I'm not sure how to get the desired result in JavaScript:

I'm trying to turn an array of paths into a JSON tree:

/

But my path is being overwritten.

I'm trying to take something like this:

[
    '/org/openbmc/path1', 
    '/org/openbmc/path2', 
    ...
]

... and turn it into...

output = {
   org: {
     openbmc: {
       path1: {},
       path2: {}
     }
   }
}

I'm sure this is pretty easy, but I'm missing something.

I found the question How to convert a file path into treeview?, but I'm not sure how to get the desired result in JavaScript:

I'm trying to turn an array of paths into a JSON tree:

https://jsfiddle.net/tfkdagzv/16/

But my path is being overwritten.

I'm trying to take something like this:

[
    '/org/openbmc/path1', 
    '/org/openbmc/path2', 
    ...
]

... and turn it into...

output = {
   org: {
     openbmc: {
       path1: {},
       path2: {}
     }
   }
}

I'm sure this is pretty easy, but I'm missing something.

Share Improve this question edited May 1, 2018 at 21:23 John Slegers 47.1k23 gold badges204 silver badges173 bronze badges asked Mar 27, 2016 at 14:07 LindsayLindsay 9272 gold badges12 silver badges30 bronze badges 1
  • 1 Could you show us your attempt(s)? – Matthijs Commented Mar 27, 2016 at 14:11
Add a comment  | 

3 Answers 3

Reset to default 12
const data = [
    "/org/openbmc/examples/path0/PythonObj",
    "/org/openbmc/UserManager/Group",
    "/org/openbmc/HostIpmi/1",
    "/org/openbmc/HostServices",
    "/org/openbmc/UserManager/Users",
    "/org/openbmc/records/events",
    "/org/openbmc/examples/path1/SDBusObj",
    "/org/openbmc/UserManager/User",
    "/org/openbmc/examples/path0/SDBusObj",
    "/org/openbmc/examples/path1/PythonObj",
    "/org/openbmc/UserManager/Groups",
    "/org/openbmc/NetworkManager/Interface"
];

const output = {};
let current;

for (const path of data) {
    current = output;

    for (const segment of path.split('/')) {
        if (segment !== '') {
            if (!(segment in current)) {
                current[segment] = {};
            }

            current = current[segment];
        }
    }
}

console.log(output);

Your solution was close, you just didn't reset the current variable properly. Use this:

current = output;

instead of this:

current = output[path[0]];

This function should do :

var parsePathArray = function() {
    var parsed = {};
    for(var i = 0; i < paths.length; i++) {
        var position = parsed;
        var split = paths[i].split('/');
        for(var j = 0; j < split.length; j++) {
            if(split[j] !== "") {
                if(typeof position[split[j]] === 'undefined')
                    position[split[j]] = {};
                position = position[split[j]];
            }
        }
    }
    return parsed;
}

Demo

var paths = [
    "/org/openbmc/UserManager/Group",
    "/org/stackExchange/StackOverflow",
    "/org/stackExchange/StackOverflow/Meta",
    "/org/stackExchange/Programmers",
    "/org/stackExchange/Philosophy",
    "/org/stackExchange/Religion/Christianity",
    "/org/openbmc/records/events",
    "/org/stackExchange/Religion/Hinduism",
    "/org/openbmc/HostServices",
    "/org/openbmc/UserManager/Users",
    "/org/openbmc/records/transactions",
    "/org/stackExchange/Religion/Islam",
    "/org/openbmc/UserManager/Groups",
    "/org/openbmc/NetworkManager/Interface"
];

var parsePathArray = function() {
    var parsed = {};
    for(var i = 0; i < paths.length; i++) {
        var position = parsed;
        var split = paths[i].split('/');
        for(var j = 0; j < split.length; j++) {
            if(split[j] !== "") {
                if(typeof position[split[j]] === 'undefined')
                    position[split[j]] = {};
                position = position[split[j]];
            }
        }
    }
    return parsed;
}

document.body.innerHTML = '<pre>' +
                          JSON.stringify(parsePathArray(), null, '\t')
                          '</pre>';

(see also this Fiddle)

NB: The resulting arrays need to be merged

This method works for both files & directories, and by using only arrays as the data format.

The structure is based upon arrays being folders, the first element being the folder name and the second - the contents array.

Files are just regular strings inside the array (but could easily be objects containing properties)

Converts =>

[
  '/home/',
  '/home/user/.bashrc',
  '/var/',
  '/var/test.conf',
  '/var/www/',
  '/var/www/index.html',
  '/var/www/index2.html'
]

To =>

[
  ['home', [
    ['user', [
      '.bashrc'
    ]]
  ]],
  ['var', [
    'test.conf',
    ['www', [
      'index.html',
      'index2.html'
    ]]
  ]]
]

 

Script:

var paths = [
  '/var/',
  '/var/test.conf',
  '/var/www/',
  '/var/www/index.html',
  '/var/www/index2.html'
]

var parsed = []

for (let path of paths) {
  let tree = path.split('/')
  let previous = parsed

  console.groupCollapsed(path)
  for (let item in tree) {
    const name = tree[item]
    const last = item == tree.length - 1

    if (name) {
      if (last) {
        console.log('File:', name)
        previous.push(name) - 1
      } else {
        console.log('Folder:', name)
        let i = previous.push([name, []]) - 1
        previous = previous[i][1]
      }  
    }
  }
  console.groupEnd(path)
}

console.warn(JSON.stringify(parsed))
发布评论

评论列表(0)

  1. 暂无评论