对js对象执行循环,然后使用map方法代替一次又一次调用组件

Performing loop on js objects and then use map method instead of calling component again and again

在 React 中需要帮助……我将复杂的 xml 文件转换为 js 对象格式。转换xml文件后,js对象存储在ress变量中,如图所示。我们将不同的部分转录本存储在不同的变量中。有没有什么办法可以使用循环来做到这一点,并在每次迭代中用正则表达式将其拆分。所以我可以很容易地使用 map 方法,而不是为每个部分转录本一次又一次地调用组件。

parser.parseString(xmlToParse, function (err, result) {
    console.log(result)
    ress = result;
  });
var regex=/ (?=\w+:)/g
var str0=ress.ROOT.record[0].index[0].point[0].partial_transcript[0];
var ptr0=str0.split(regex)
var str1=ress.ROOT.record[0].index[0].point[1].partial_transcript[0];
var ptr1=str1.split(regex)
var str2=ress.ROOT.record[0].index[0].point[2].partial_transcript[0];
var ptr2=str2.split(regex)
var str3=ress.ROOT.record[0].index[0].point[3].partial_transcript[0];
var ptr3=str3.split(regex)

console
ROOT:
$: {xmlns: 'https://www.weareavp.com/nunncenter/ohms', xmlns:xsi: 'http://www.w3.org/2001/XMLSchema-instance', xsi:schemaLocation: 'https://www.weareavp.com/nunncenter/ohms/ohms.xsd'}
SCRIPT: [{…}]
record: Array(1)
0:
$: {id: '00107334', dt: '2021-11-01'}
accession: ['OHP-0020']
cms_record_id: ['OHP-0020']
collection_id: ['']
collection_link: ['']
collection_name: ["'Bristow Historical Society - Oral History Archive'"]
date: [{…}]
date_nonpreferred_format: ['Unknown Date']
description: ['']
duration: ['0:00-1:04:33']
file_name: ['']
fmt: ['audio']
format: ['MP3']
funding: ['']
index: Array(1)
0:
point: Array(14)
0:
gpspoints: [{…}]
hyperlinks: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
keywords: ['Arthur Foster;Claire Diehl;Country Club;Edith Abbo…ity;Ranny Foster;Skinner Barn;Superstitious;Tulsa']
keywords_alt: ['']
partial_transcript: Array(1)
0: "NF : Ms. Mills , we’re so happy that you had us today. Let us come and talk to you about this, because I have a feeling you have information and things that happened that maybe nobody else that we’ve come in contact with would even know. EM: I’ll read this first and see if there’s anything before you record. NF: Okay. EM: Now well, I didn’t know whether you don’t need to leave Mr. Mills name or anything like that but that’s what I had on the recording— NF: Uh-huh. EM: I mean on my history. He came here to this area in 1890 from Guthrie and he helped lay the Frisco Railroad road bed. He— by hauling ties with his mule team, between Tulsa and Oklahoma City. They— he and his brother— first his two brothers and one brother dropped out. They lived on deer meat and wild turkey which were plentiful. The deer came up to the door. They hated to kill the deer because they came up for salt— NF: Mm-hmm. EM: —and they could just rope them and they had their deer meat. NF: Wow. EM: Or salt and let’s see— which were plentiful. The deer came up to the door for salt and the wild turkeys roosted in trees at night. They’d catch all they wanted at night. Indians taught them how to make (Indecipherable) from corn. So they had plenty of meat and then they had the (Indecipherable) that the Indians taught them to make. Then here in Bristow, I had a note here on the old Skinner Barn was located right down here. "
length: 1
[[Prototype]]: Array(0)
partial_transcript_alt: ['']
subjects: ['']
subjects_alt: ['']
synopsis: ['']
synopsis_alt: ['']
time: ['451']
title: ['Introduction and History Read Aloud']
title_alt: ['']
[[Prototype]]: Object
1: {time: Array(1), title: Array(1), title_alt: Array(1), partial_transcript: Array(1), partial_transcript_alt: Array(1), …}
2: {time: Array(1), title: Array(1), title_alt: Array(1), partial_transcript: Array(1), partial_transcript_alt: Array(1), …}
3: {time: Array(1), title: Array(1), title_alt: Array(1), partial_transcript: Array(1), partial_transcript_alt: Array(1), …}

你可以试试这个:

parser.parseString(xmlToParse, function (err, result) {
    console.log(result)
    ress = result;
  });
var regex=/ (?=\w+:)/g

//this object will contain all the variabels with the numbers and the names
const object = {}
//easier to read
const ress = ress.ROOT.record[0].index[0].point
//while loop will do the work
let i = 0
while(i < ress.length) {
  const str = ress[i].partial_transcript[0];
  object[`ptr`+i] = str.split(regex)
  i++
}