特殊格式字符串转为对象数组



Name                       : 以太网 3
InterfaceDescription       : Cisco AnyConnect Secure Mobility Client Virtual Miniport Adapter for Windows x64
InterfaceIndex             : 11
MacAddress                 : 00-05-9A-3C-7A-00
MediaType                  : 802.3
PhysicalMediaType          : Unspecified
InterfaceOperationalStatus : NotPresent
AdminStatus                : Down
LinkSpeed(Mbps)            : 0
MediaConnectionState       : Unknown
ConnectorPresent           : False
DriverInformation          : Driver Date 2014-02-26 Version 3.1.6019.0 NDIS 6.20

Name                       : 以太网 2
InterfaceDescription       : XenServer PV Network Device #0
InterfaceIndex             : 8
MacAddress                 : CA-9D-17-AD-A0-49
MediaType                  : 802.3
PhysicalMediaType          : Unspecified
InterfaceOperationalStatus : Up
AdminStatus                : Up
LinkSpeed(Gbps)            : 1
MediaConnectionState       : Connected
ConnectorPresent           : True
DriverInformation          : Driver Date 2017-07-31 Version 8.2.1.102 NDIS 6.1

js或jquery 如何将这种特殊格式的纯字符串转为对象数组,求指教!

img

代码如下可以试试:

    this.dataString.split('\n').forEach((v, index) => {
      const array = v.split(':');
      if (!v) {
        result.push({});
        return;
      }
      result[result.length - 1][array[0].trim()] = array[1].trim();
    });
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge,chrome=1" />
    <meta name="renderer" content="webkit" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <title>html</title>
  </head>
  <body>
    <script>
    //   此方案一般,不是最佳,且需要满足一定的条件;
    //   1.在每行末尾添加英文字符',';
    //   2.固定格式,每一大项以Name 行开始,DriverInformation行结束;
    //   3.Name行开始至DriverInformation行结束,数量为12行;
      let str = `
            Name                       : 以太网 1,
            InterfaceDescription       : Cisco AnyConnect Secure Mobility Client Virtual Miniport Adapter for Windows x64,
            InterfaceIndex             : 11,
            MacAddress                 : 00-05-9A-3C-7A-00,
            MediaType                  : 802.3,
            PhysicalMediaType          : Unspecified,
            InterfaceOperationalStatus : NotPresent,
            AdminStatus                : Down,
            LinkSpeed(Mbps)            : 0,
            MediaConnectionState       : Unknown,
            ConnectorPresent           : False,
            DriverInformation          : Driver Date 2014-02-26 Version 3.1.6019.0 NDIS 6.20,
            Name                       : 以太网 2,
            InterfaceDescription       : XenServer PV Network Device #0,
            InterfaceIndex             : 8,
            MacAddress                 : CA-9D-17-AD-A0-49,
            MediaType                  : 802.3,
            PhysicalMediaType          : Unspecified,
            InterfaceOperationalStatus : Up,
            AdminStatus                : Up,
            LinkSpeed(Gbps)            : 1,
            MediaConnectionState       : Connected,
            ConnectorPresent           : True,
            DriverInformation          : Driver Date 2017-07-31 Version 8.2.1.102 NDIS 6.1,
            Name                       : 以太网 3,
            InterfaceDescription       : XenServer PV Network Device #0,
            InterfaceIndex             : 8,
            MacAddress                 : CA-9D-17-AD-A0-49,
            MediaType                  : 802.3,
            PhysicalMediaType          : Unspecified,
            InterfaceOperationalStatus : Up,
            AdminStatus                : Up,
            LinkSpeed(Gbps)            : 1,
            MediaConnectionState       : Connected,
            ConnectorPresent           : True,
            DriverInformation          : Driver Date 2017-07-31 Version 8.2.1.102 NDIS 6.1
      `;
      //先将字符串以逗号分割并去除首尾空格再转换为数组
      let arr1 = str.split(",").map((item) => {
        item = item.trim().split(":");
        return {
          [item[0].trim()]: item[1].trim(),
        };
      });
      console.log(arr1);//此时输出的结果共有36项
      let arr2 = [];
      //将Name字段开始至DriverInformation字段结束之间设置为1项,以本案例为例,共有三项
      arr1.forEach((item, index) => {
        if ('Name' in item) {//如果当前项中有Name字段
           arr2.push(Object.assign({}, ...arr1.slice(index, index + 12)));
        }
      });
      console.log('arr2',arr2);
    </script>
  </body>
</html>

img