I have my array myData= "1|N|BK1||MATH|1890.00|2100.00|1: 2|N|BK2||SCIENCE|1520.00|1790.00|1: "
I need to modify this myData values as below:
myFinalArray=
[
{
"p_s.no": "3",
"p_ebook": "N",
"p_id": "BK1",
"p_ebooktitle": "",
"p_name": "MATH",
"p_cprice": "1890.00",
"p_lprice": "2100.00",
"p_Q": "1"
},
{
"p_s.no": "3",
"p_ebook": "N",
"p_id": "BK2",
"p_ebooktitle": "",
"p_name": "SCIENCE",
"p_cprice": "1890.00",
"p_lprice": "2100.00",
"p_Q": "1"
}
]
I am trying to split the values with ":" & then "|" and loop through each values to add a text for each values. Finally pushing it to a variable. I feel there might be some better ways to achieve this. Please help me with your ideas.
Sample code below:
this.myDataArray = this.myData.split(": ")
this.myDataArray.forEach(element=>
{
this.myNewDataArray=element.split("|")
for (let i = 0; i < this.myNewDataArray.length; i++)
{
if(i===0){
this.temp = ('"p_s.no"' + ':' + '"' + this.myNewDataArray[i] + '"');
}
else if (i === 1) {
this.temp = this.temp + "," + ('"p_ebook"' + ':' + '"' + this.myNewDataArray[i] + '"');
}
else if (i === 2) {
this.temp = this.temp + "," + ('"p_id"' + ':' + '"' + this.myNewDataArray[i] + '"');
}
else if (i === 3) {
this.temp = this.temp + "," + ('"p_ebooktitle"' + ':' + '"' + this.myNewDataArray[i] + '"');
}
else if (i === 4) {
this.temp = this.temp + "," + ('"p_name"' + ':' + '"' + this.myNewDataArray[i] + '"');
}
else if (i === 5) {
this.temp = this.temp + "," + ('"p_cprice"' + ':' + '"' + this.myNewDataArray[i] + '"');
}
else if (i === 6) {
this.temp = this.temp + "," + ('"p_lprice"' + ':' + '"' + this.myNewDataArray[i] + '"');
}
else if (i === 7) {
this.temp = this.temp + "," + ('"p_Q"' + ':' + '"' + this.myNewDataArray[i] + '"');
}
}
if (this.temp !== "") {
this.myFinalArray.push("{" + this.temp + "}");
}
})