将字符串更改为数组

时间:2018-08-30 15:09:07

标签: javascript json

我检索以下字符串值数组

[
"Module1.resource1.create",
"Module1.resource1.read",
"Module1.resource1.update",
"Module1.resource1.delete",
...
"Module1.resourceN.create",
"Module1.resourceN.read",
"Module1.resourceN.update",
]

我想将它们更改为json格式

{"privileges": {
    "Module1": {
      "resource1": {
          "create": true,
          "read": true,
          "update": true,
          "delete": true,
      },
...
      "resourceN":  {
          "create": true,
          "read": true,
          "update": true,
    }
  }
 }
}

我能想到的是为每个字符串运行一个循环,使用split("."),然后以某种方式将其更改为json对象。我遇到的问题是json不是简单的key: value,如果有意义的话,它更像是key: object: object: object: value ...

我希望朝正确的方向前进。

欢呼

5 个答案:

答案 0 :(得分:2)

您可以使用Array#reduce方法:

const result = { privileges: {} };
items.reduce((ret, el) => {
   // Split the array elements and use destructuring assignment syntax for
   // creating constants
   const [module, resource, verb] = el.split('.');
   // check if privileges[module] property exists. If not, define it as an empty object.
   if (!ret[module]) ret[module] = {};
   // follows the previous line's logic for resources!
   if (!ret[module][resource]) ret[module][resource] = {};
   ret[module][resource][verb] = true;
   return ret;
}, result.privileges);

Demo | Destructuring assignment syntax

答案 1 :(得分:1)

使用点对象可能会有所帮助,而无需重新发明轮子:

https://github.com/rhalff/dot-object

答案 2 :(得分:1)

这花了一段时间,但完成了工作:

const rules = [
    "Module1.resource1.create",
    "Module1.resource1.read",
    "Module1.resource1.update",
    "Module1.resource1.delete",
    "Module1.resourceN.create",
    "Module1.resourceN.read",
    "Module1.resourceN.update",
  ].map(rule => rule.split('.'));
  
const res = { privileges: {} };

for (const rule of rules) {
  for (i = 0; i < rule.length; i++) {
    switch (i) {
      case 0: 
        res.privileges[rule[0]] = Object.assign({}, res.privileges[rule[0]])
        break;
      case 1:
        res.privileges[rule[0]][rule[1]] = Object.assign({}, res.privileges[rule[0]][rule[1]])
        break;
      case 2:
        res.privileges[rule[0]][rule[1]][rule[2]] = true
      default:
    }
  }
}

console.log(res)

答案 3 :(得分:0)

为此,您可以使用以下功能:

function setIn(target, path, value) {
  var chain = String(path).split('.');
  while (chain.length > 0) {
    var field = chain.shift();
    if (chain.length == 0) {
      target[field] = value;
    } else if (field in target && target[field] !== null && typeof target[field] === 'object') {
      target = target[field];
    } else {
      target[field] = {}
      target = target[field];
    }
  }
  return value;
}

并生成树:

var tree = { privileges: {} };
for (var i = 0; i < array.length; i += 1)
  setIn(tree.privileges, array[i], true);

答案 4 :(得分:0)

为此,您可以使用{ "name": "Remote", "type": "go", "request": "launch", "mode": "remote", "remotePath": "${workspaceRoot}", "port": 2345, "host": "127.0.0.1", "program": "${workspaceRoot}", "env": {} } 来创建嵌套地图。上面的示例适用于任何级别的子级,并且始终将最后一个子级设置为布尔值。

Array.reduce()