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



Name                       : 以太网 3
InterfaceDescription       :""
InterfaceIndex             : 11
MacAddress                 : ""
MediaType                  : 802.3
PhysicalMediaType          : Unspecified
InterfaceOperationalStatus : NotPresent
AdminStatus                : Down
LinkSpeed(Mbps)            : 0
MediaConnectionState       : Unknown
ConnectorPresent           : False
DriverInformation          :""

Name                       : 以太网 2
InterfaceDescription       : ""
InterfaceIndex             : 8
MacAddress                 : ""
MediaType                  : 802.3
PhysicalMediaType          : Unspecified
InterfaceOperationalStatus : Up
AdminStatus                : Up
LinkSpeed(Gbps)            : 1
MediaConnectionState       : Connected
ConnectorPresent           : True
DriverInformation          :""

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

function strToArray(str) {
  const res = []
  const groups = str.split(/\n/)
  let obj = {}
  for (const group of groups) {
    const groupWithoutWhite = group.replace(/\s+/g, '')
    if (groupWithoutWhite) {
      const [ key, value ] = groupWithoutWhite.replace(/(""|'')/g, '').split(':')
      obj[key] = value
    } else {
      if (Object.keys(obj).length) {
        res.push(obj)
      }
      obj = {}
    }
  }
  if (Object.keys(obj).length) {
    res.push(obj)
  }
  return res
}

const str = `
Name                       : 以太网 3
InterfaceDescription       :
InterfaceIndex             : 11
MacAddress                 : *
MediaType                  : 802.3
PhysicalMediaType          : Unspecified
InterfaceOperationalStatus : NotPresent
AdminStatus                : Down
LinkSpeed(Mbps)            : 0
MediaConnectionState       : Unknown
ConnectorPresent           : False
DriverInformation          : ""

Name                       : 以太网 2
InterfaceDescription       :
InterfaceIndex             : 8
MacAddress                 : **
MediaType                  : 802.3
PhysicalMediaType          : Unspecified
InterfaceOperationalStatus : Up
AdminStatus                : Up
LinkSpeed(Gbps)            : 1
MediaConnectionState       : Connected
ConnectorPresent           : True
DriverInformation          : ""`


console.log(strToArray(str))
<script>
let str = `
Name                       : 以太网 3
InterfaceDescription       :""
InterfaceIndex             : 11
MacAddress                 : ""
MediaType                  : 802.3
PhysicalMediaType          : Unspecified
InterfaceOperationalStatus : NotPresent
AdminStatus                : Down
LinkSpeed(Mbps)            : 0
MediaConnectionState       : Unknown
ConnectorPresent           : False
DriverInformation          :""
 
Name                       : 以太网 2
InterfaceDescription       : ""
InterfaceIndex             : 8
MacAddress                 : ""
MediaType                  : 802.3
PhysicalMediaType          : Unspecified
InterfaceOperationalStatus : Up
AdminStatus                : Up
LinkSpeed(Gbps)            : 1
MediaConnectionState       : Connected
ConnectorPresent           : True
DriverInformation          :""
`
let reg =/([^\n\r]+)[:]([^\n\r]+)/igs;
let res
// 保存信息的数组
let movieArr = []
// 循环匹配
while (res = reg.exec(str)) {
  let obj = {
    name: res[1],
    url: res[2]
  }
  movieArr.push(obj)
}
console.log(movieArr)
</script>


var str = `
Name                       : 以太网 3
InterfaceDescription       :""
InterfaceIndex             : 11
MacAddress                 : ""
MediaType                  : 802.3
PhysicalMediaType          : Unspecified
InterfaceOperationalStatus : NotPresent
AdminStatus                : Down
LinkSpeed(Mbps)            : 0
MediaConnectionState       : Unknown
ConnectorPresent           : False
DriverInformation          :""

Name                       : 以太网 2
InterfaceDescription       : ""
InterfaceIndex             : 8
MacAddress                 : ""
MediaType                  : 802.3
PhysicalMediaType          : Unspecified
InterfaceOperationalStatus : Up
AdminStatus                : Up
LinkSpeed(Gbps)            : 1
MediaConnectionState       : Connected
ConnectorPresent           : True
DriverInformation          :""
`
var data = str.split(/\s\n/)
var arr = []
data.forEach(i => {
    let item = {}
    i.split(/[\r\n\t]/).filter(i=>i)
                 .forEach(j => {
                    
                    let [key, value] = j.replace(/\s*/g,"").split(':')
                    if (value === '""') value = ''
                    item[key] = value
                 })
    
   arr.push(item)
})
console.log('输出转化后的数组', arr)

var str = `
Name                       : 以太网 3
InterfaceDescription       :""
InterfaceIndex             : 11
MacAddress                 : ""
MediaType                  : 802.3
PhysicalMediaType          : Unspecified
InterfaceOperationalStatus : NotPresent
AdminStatus                : Down
LinkSpeed(Mbps)            : 0
MediaConnectionState       : Unknown
ConnectorPresent           : False
DriverInformation          :""
Name                       : 以太网 2
InterfaceDescription       : ""
InterfaceIndex             : 8
MacAddress                 : ""
MediaType                  : 802.3
PhysicalMediaType          : Unspecified
InterfaceOperationalStatus : Up
AdminStatus                : Up
LinkSpeed(Gbps)            : 1
MediaConnectionState       : Connected
ConnectorPresent           : True
DriverInformation          :""
`
var data = str.split(/\s\n/)
var arr = []
data.forEach(i => {
    let item = {}
    i.split(/[\r\n\t]/).filter(i=>i)
                 .forEach(j => {
                    
                    let [key, value] = j.replace(/\s*/g,"").split(':')
                    if (value === '""') value = ''
                    if (key in item) {
                        arr.push(item)
                        item = {}
                    }
                    item[key] = value
                 })
    
   arr.push(item)
})
console.log(arr)

输出结果:

img

var data = str.split(/\s\n/)
var arr = []
data.forEach(i => {
let item = {}
i.split(/[\r\n\t]/).filter(i=>i)
.forEach(j => {

                let [key, value] = j.replace(/\s*/g,"").split(':')
                if (value === '""') value = ''
                if (key in item) {
                    arr.push(item)
                    item = {}
                }
                item[key] = value
             })

arr.push(item)
})
console.log(arr)



先按行读取内容 对每一行进行遍历 用“:”进行split切割分组,第一个值就是key 第二个就是value 接着去掉空格,然后用new { key1:key,value1:value }