0

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 + "}");        
      }
})

1 Answer 1

2

Put all your object keys in an array so you can iterate the array to match each property name and value in an object within a map() of the split data string

const keys = ["p_s.no","p_ebook","p_id","p_ebooktitle","p_name","p_cprice","p_lprice","p_Q"];

const myData= "1|N|BK1||MATH|1890.00|2100.00|1: 2|N|BK2||SCIENCE|1520.00|1790.00|1: ";

const res = myData.split(': ')
                  .filter(s => !!s.trim())
                  .map(s => s.split('|').reduce((a,c,i) => (a[keys[i]] = c, a),{}))
                  
console.log(res)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.