什么是轉換純文本資料的最佳方式?
[group a)
a
b
c
[b組]
a
d
f
進入一個物件...
{
groupA: ['a'/span>, 'b'/span>, 'c'/span>]。
groupB: ['a'/span>, 'd', 'f']
}
純文本資料的長度會有所不同,但其結構將始終被表示為:
[父組] 。
子專案1
子專案2。
...
uj5u.com熱心網友回復:
你可以使用分割和減少
。const string = `[group a]
a
b
c
[b組]
a
d
f`
const list = string.split(`/span>)
`)
let saveKey;
const obj = list.reduce((acc,cur) => {
if (cur.startsWith('['/span>)) {
const groupLetter = cur. match(/ (w )]/)[1].toUpperCase()
saveKey = `group${groupLetter}`。
acc[saveKey]=[]。
}
else {
acc[saveKey].push(cur)。
}
return acc
},{})
console.log(obj)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
你可以抓取所有的文本行,并通過決議和判斷它是一個鍵還是一個值來減少行。你的累加器需要同時存盤結果和當前的鍵。
。const
parse = (text) =>
text.trim().split('
')
.reduce((acc, line) =>/span> {
const match = line.trim().match(/^[(. )]$/) 。
return matching
? { ...acc, key: match[1] } 。
: { ...acc, result: { ...acc.result。
[acc.key]: [...(acc.result[acc.key] ? []), line.trim()] }
};
}, { result: {}, key: null }).result。
data = parse(document.querySelector('#data'/span>).value)。
console.log(data);
.as-console-wrapper { top: 0; max-height: 100% ! important; }
#data { display: none; }
< textarea id="data">【group a
a
b
c
[b組]
a
d
f</textarea>/span>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
通過在上進行分割,制作一個原始字串的陣列。
,然后回圈并創建物件:
const str = `[group a]
a
b
c
[b組]
a
d
f`
const arr = str.split('
')
const obj = {}。
let curHeader
arr.forEach(span class="hljs-params">el =>)
{
if(el[0] === ' [')
{
let frmtEl = el.substring(1, el.length-1)
let frmtElArr = frmtEl.split(')
curHeader = frmtElArr[0] frmtElArr[1].toUpperCase()
obj[curHeader] = [] 。
}
else
{
obj[curHeader].push(el)
}
})
console.log(obj)
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
(1) 首先,用split()作為分隔符,應該可以得到與字串中一樣多的組:
[ '', 'group a] 。
a
b
c
', 'groupe b].
a
d
f' ]
(2) 使用.filter()來洗掉不需要的空字串:
[ 'group a] 。
a
b
c
', 'group b] , 'group b] a
a
d
f' ]
(3) 現在使用.map()來轉換每個組,使用]或者
作為分隔符:
[ 'group a', ''/span>, 'a', 'b', 'c', '' ], [ 'b組', '', 'a'/span>, 'd'/span>, 'f'/span> ] ]
(4) 使用.filter()來洗掉不想要的空字串:
[ 'group a'/span>, 'a'/span>, 'b'/span>, 'c' ], ['group b', 'a', 'd', 'f'] ]
(5) 最后使用.reduce()和Object.assign()將每個組轉換成一個鍵值對:
{ 'group a'/span>: ['a', 'b', 'c'], '組 b': ['a', 'd', 'f']. }
請注意該片段,
.split(')。 map((a,i) => i === 0 ? a : a.toUpperCase().join(''/span>)
是將group a轉換為groupA,等等
DEMO
。let data = `[group a]
a
b
c
[b組]
a
d
f
[c組]
g
h
t`。
let obj = data
.split(/[/)
.filter(v => v)
.map(v => v.split(/]|[
] /)
.filter(v => v))
.reduce((acc,val) =>
Object.assign(acc,
{[val[0].split(')。 map((a,i) => i === 0 ? a : a.toUpperCase().join(''/span>)]:val. slice(1)}),{}) 。
console.log( obj );
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/313035.html
標籤:
