Javascript数组到对象

Ale*_*hel 7 javascript

我有一个看起来像这样的数组:

files = [
  'Dashboard/Logs/Errors',
  'Dashboard/Logs/Other',
  'Accounts/Main',
]
Run Code Online (Sandbox Code Playgroud)

我想让它看起来像这样:

navigation = [
  {
    "title": "Dashboard",
    "dropdown": [
      {
        "title": "Logs",
        "dropdown": [
          {
            "title": "Errors",
          },
          {
            "title": "Other",
          }
        ]
      }
    ]
  },
  {
    "title": "Accounts",
    "dropdown": [
      {
        "title": "Main",
      }
    ]
  }
]
Run Code Online (Sandbox Code Playgroud)

到目前为止,我有以下内容:

var navigation = [];
for (var i = 0; i < files.length; i++) {
  var parts = files[i].split('/');
  navigation.push({title: parts[0]});
  for (var j = 1; j < parts.length; j++) {

  }
}
Run Code Online (Sandbox Code Playgroud)

我很难找到一个体面的方法来做到这一点.到目前为止我已经无法工作,因为它在导航下创建了两个对象title: "Dashboard".任何想法聪明的方法?谢谢 :)

arc*_*rty 7

这应该产生所需的输出:

var files = [
  'Dashboard/Logs/Errors',
  'Dashboard/Logs/Other',
  'Accounts/Main',
];

var navigation = [];
// Iterates through a navigation array and returns the object with matching title, if one exists.
var getNavigationObject = function(nav, title) {
  for (var i = 0; i < nav.length; i++) {
    if (nav[i].title == title) {
      return nav[i];
    }
  }
};
// Adds a file to the nav.
// The input is an array of file components (i.e. file.split('/'))
// This works by recursively adding each component of a file.
var addToNav = function (nav, components) {
  var n = getNavigationObject(nav, components[0]);
  if (!n) {
    n = {
      title: components[0]
    };
    nav.push(n);
  }
  if (components.length > 1) {
    n.dropdown = n.dropdown || [];
    addToNav(n.dropdown, components.slice(1));
  }
};

// Actually call `addToNav` on each file.
files.forEach(function(e) {
  addToNav(navigation, e.split('/'));
});

// Produces the result in string form.
JSON.stringify(navigation, null, 2)
Run Code Online (Sandbox Code Playgroud)

这通过递归检查给定元素是否已经与文件的组件匹配来工作.如果是,它会重新进入该组件的"下拉列表".否则,它会创建它.