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

Time:11-15

Need help in React…I converted the complex xml file into js object format. After converting the xml file, the js object are stored in ress variable that is showing in the picture. We are storing the different partial transcript in different variables. Is there any way to do it using loop and split it with regular expression in each iteration. So it would he easy for me to use map method instead of calling the component again and again for each partial transcript.

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), …}

CodePudding user response:

you can try this :

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  
}

  • Related