一只开发狗001 2022-09-28 08:18 采纳率: 25%
浏览 30
已结题

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



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 如何将这种特殊格式的纯字符串转为对象数组,求指教!

  • 写回答

2条回答 默认 最新

  • 土豆有毒 2022-09-29 02:24
    关注

    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();
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)
编辑
预览

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 9月29日
  • 已采纳回答 9月29日
  • 创建了问题 9月28日