根据字符串值数组创建对象

时间:2019-09-13 11:13:40

标签: javascript node.js

我需要基于字符串值的数组更新对象名称,并且最后一个字符串值应为数组。

我使用array.forEach循环,但是我不知道如何在对象内部找到对象(如果存在)并且myArray包含大约10,000个字符串。

const myArray = [
  '/unit/unit/225/unit-225.pdf',
  '/nit/nit-dep/4.11/nit-4.11.pdf',
  '/nit/nit-dep/4.12/nit-4.12.pdf',
  '/org/viti/viti-engine/5.1/viti-engine-5.1.pdf',
  '/org/viti/viti-spring/5.1/viti-spring-5.1.pdf'
];
var parentObject = {}
myArray.forEach(res => {
  res = res.slice(1, res.length);
  var array = res.split("/");
  array.forEach((e, i) => {
    ........ // here I am confused 

  });
})

最终输出应为

parentObject = {
  'unit': {
    'unit': {
      '225': {
        'unit-225.pdf': []
      }
    }
  },
  'nit': {
    'nit-dep': {
      '4.11': {
        'nit-4.11.pdf': []
      },
      '4.12': {
        'nit-4.12.pdf': []
      }
    }
  },
  'org': {
    'viti': {
      'viti-engine': {
        '5.1': {
          'viti-engine-5.1.pdf': []
        }
      },
      'viti-spring': {
        '5.2': {
          'viti-engine-5.2.pdf': []
        }
      }
    }
  }
}

3 个答案:

答案 0 :(得分:3)

用斜线分隔后,请使用imageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ((MainActivity)getActivity()).openDrawer(); } } 迭代嵌套对象,必要时首先创建每个嵌套属性,然后将数组分配给filename属性:

reduce

答案 1 :(得分:2)

您可以缩小数组,也可以缩小路径。最后分配数组。

const
    array = ['/unit/unit/225/unit-225.pdf', '/nit/nit-dep/4.11/nit-4.11.pdf', '/nit/nit-dep/4.12/nit-4.12.pdf', '/org/viti/viti-engine/5.1/viti-engine-5.1.pdf', '/org/viti/viti-spring/5.1/viti-spring-5.1.pdf'],
    result = array.reduce((r, path) => {
        var keys = path.split(/\//).slice(1),
            last = keys.pop();

        keys.reduce((o, k) => o[k] = o[k] || {}, r)[last] = [];
        return r;
    }, {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

一种更快的方法。

const
    array = ['/unit/unit/225/unit-225.pdf', '/nit/nit-dep/4.11/nit-4.11.pdf', '/nit/nit-dep/4.12/nit-4.12.pdf', '/org/viti/viti-engine/5.1/viti-engine-5.1.pdf', '/org/viti/viti-spring/5.1/viti-spring-5.1.pdf'],
    result = {};

for (let path of array) {
    let keys = path.split(/\//).slice(1),
        last = keys.pop(),
        temp = result;

    for (let key of keys) {
        temp[key] = temp[key] || {};
        temp = temp[key];
    }
    temp[last] = [];
}

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)

您还可以采用递归方法。

只需不断移动拆分路径,直到获得每个分支的最终分配。

const myArray = [
  '/unit/unit/225/unit-225.pdf',
  '/nit/nit-dep/4.11/nit-4.11.pdf',
  '/nit/nit-dep/4.12/nit-4.12.pdf',
  '/org/viti/viti-engine/5.1/viti-engine-5.1.pdf',
  '/org/viti/viti-spring/5.1/viti-spring-5.1.pdf'
];

console.log(buildTree(myArray));

function buildTree(list=[]) {
  return list.reduce((node, item) => buildBranches(node, item.split(/\//g).filter(x => x !== '')), {});
}

function buildBranches(node={}, rest=[]) {
  let key = rest.shift();
  node[key] = rest.length < 2 ? { [rest.shift()] : [] } /** or rest.shift() */ : node[key] || {};
  if (rest.length > 1) buildBranches(node[key], rest);
  return node;
}
.as-console-wrapper { top: 0; max-height: 100% !important; }