使用字符串数组创建多级对象,将数组解析为JS对象

时间:2022-10-10 07:07:31

I have this array that needs to be parsed into a useful object. The names of each value are a collection of namespaces separated by / characters. The values between each '/' need to be turned into a JS Objects property:

我有这个数组需要解析成一个有用的对象。每个值的名称是由/字符分隔的名称空间的集合。每个'/'之间的值需要转换为JS Objects属性:

"status": [
  {
    "message": "OK",
    "name": "/Computer",
    "values": []
  },
  {
    "name": "/Computer/CPU Usage",
    "values": []
  },
  {
    "name": "/Computer/CPU Temp",
    "values": []
  },
  {
    "name": "/Computer/hardware/memory",
    "values": []
  }
]

I need it to become this:

我需要它成为这个:

"status": {
  "computer": {
      "CPU Usage": {
        "values": []
      },
      "CPU Temp": {
        "values": []
      },
      "hardware": {
        "memory": {
          "values": []
        }
      }
    }
  }

So far I have done this:

到目前为止,我已经这样做了:

var statii = status, // from above..
    _parsedStatii = {};

for (var i = 0; statii.length < 0; i ++) {
    var _nameSpaces = statii[i].name.split('/');

    // Start at 1 because index 0 is empty (before the first slash)
    if (!_parsedStatii[_nameSpaces[1]]) {
        _parsedStatii[_nameSpaces[1]] = {};
    }

    if (!_parsedStatii[_nameSpaces[1]][_nameSpaces[2]])
        _parsedStatii[_nameSpaces[1]][_nameSpaces[2]] = {};


    if (!_parsedStatii[_nameSpaces[1]][_nameSpaces[2]][_nameSpaces[3]])
        _parsedStatii[_nameSpaces[1]][_nameSpaces[2]][_nameSpaces[3]] = {};


    if (!_parsedStatii[_nameSpaces[1]][_nameSpaces[2]][_nameSpaces[3]][_nameSpaces[4]])
        _parsedStatii[_nameSpaces[1]][_nameSpaces[2]][_nameSpaces[3]][_nameSpaces[4]] = {};

}

Obviously it is no where near right, I have tried a lot of recursive functions but am at a bit of a loss. This example gives the clearest representation of what I am trying to achieve. Any ideas? (Please excuse code typos, it was paraphrased)

显然它没有接近正确的地方,我已经尝试了很多递归函数但是有点亏本。这个例子给出了我想要实现的最清晰的表示。有任何想法吗? (请原谅代码拼写错误,它被解释了)

1 个解决方案

#1


5  

You could split the name and build an object upon.

您可以拆分名称并构建一个对象。

var data = { "status": [{ "message": "OK", "name": "/Computer", "values": [] }, { "name": "/Computer/CPU Usage", "values": [] }, { "name": "/Computer/CPU Temp", "values": [] }, { "name": "/Computer/hardware/memory", "values": [] }] },
    object = {};

data.status.forEach(function (a) {
    a.name.slice(1).split('/').reduce(function (o, k) {
        return o[k] = o[k] || {};
    }, object).values = a.values;
});

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

#1


5  

You could split the name and build an object upon.

您可以拆分名称并构建一个对象。

var data = { "status": [{ "message": "OK", "name": "/Computer", "values": [] }, { "name": "/Computer/CPU Usage", "values": [] }, { "name": "/Computer/CPU Temp", "values": [] }, { "name": "/Computer/hardware/memory", "values": [] }] },
    object = {};

data.status.forEach(function (a) {
    a.name.slice(1).split('/').reduce(function (o, k) {
        return o[k] = o[k] || {};
    }, object).values = a.values;
});

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