Question

In: Computer Science

Hi, I need to finish function purser,which will include header row and i can get same...

Hi, I need to finish function purser,which will include header row and i can get same output

/*

[

{ firstName: 'moe', lastName: 'green' },

{ firstName: 'lucy', lastName: 'liu' },

{ firstName: 'ethyl', lastName: 'mertz' }

]*/

In javascript please

const parser=(d)=>{

let arr=[];

let newArr=[];

let obj={}

let items=d.split('|');

for (let i=0;i<items.length;i++){

arr[i]=items[i].split(',');

}

for (let z=0;z<arr.length;z++){

newArr=newArr.concat(arr[z]);

}

for(var y = 0;y < newArr.length;y+=2){

obj[newArr[y]]= newArr[y+1];

}

//const res = newArr.reduce((a,b)=> (a[b]='',a),{});

return obj;

}

const data = `firstName, lastName | moe, green|lucy, liu|ethyl, mertz`;

console.log(parser(data));

/*

[

{ firstName: 'moe', lastName: 'green' },

{ firstName: 'lucy', lastName: 'liu' },

{ firstName: 'ethyl', lastName: 'mertz' }

]*/

Solutions

Expert Solution

Below is the correct code to parse the input and output the data in required mentioned format : -

There are some modification done in the above provided code to make it correct , see below code-

===============================CODE START =====================================


const parser=(d)=>{

let arr=[];

let newArr=[];

let finalarry = []; /* Declared to store the final output , as output needs to be in array of objects format*/

let items=d.split('|');


for (let i=0;i<items.length;i++){

arr[i]=items[i].split(',');

}

/* Modification in below loop , as now it start from index 1 , because in array - arr[0] position headers are present */

for (let z=1;z<arr.length;z++){  

newArr=newArr.concat(arr[z]);

}

/* Modification in below loop */

for(var y = 0;y < newArr.length;y+=2){
let obj={} ; /* define obj within loop */   
obj[arr[0][0]] = newArr[y]; /* arr[0][0] and arr[0][1] contains the header name */
obj[arr[0][1]] = newArr[y+1];

finalarry.push(obj); /* push the created object in final array */
}

//const res = newArr.reduce((a,b)=> (a[b]='',a),{});

return finalarry; /* return the final array */

}

const data = `firstName, lastName | moe, green|lucy, liu|ethyl, mertz`;

console.log(parser(data));

=============================CODE END===============================================

Please comment if any query!


Related Solutions

c++: I need #include <iostream> header A palindrome (Links to an external site.) is a word,...
c++: I need #include <iostream> header A palindrome (Links to an external site.) is a word, phrase, number, or sequence of words that reads the same backward as forward. Punctuation, capitalization, and spaces between the words or lettering are allowed. Here are some examples of word and phrase palindromes. Words: Civic Kayak Level Madam Mom Noon Racecar Radar Refer Rotor Phrases: Don't nod. I did, did I? My gym Step on no pets Top spot Never odd or even Eva,...
Hi, i need flowchart for this code (C++) please, THANX #include <iostream> #include <thread> #include <unistd.h>...
Hi, i need flowchart for this code (C++) please, THANX #include <iostream> #include <thread> #include <unistd.h> #include <semaphore.h> #include <pthread.h> using namespace std; #define NRO 6 // Número de coches //Puente declarado con matriz y valor entero void Puente(string array, int value); // Variable global int Norte = 1; int Sur = 1; sem_t mutex1; //Coche al norte void* NorteC(void* arg){ sem_wait(&mutex1); string array = "En el lado Norte "; // Norte cout<<array<<"del puente, el coche #"<<Norte<<" puede cruzar el...
Hi! I wrote two function to get count cluster of char in a string , charFreq(),...
Hi! I wrote two function to get count cluster of char in a string , charFreq(), and an other one which iterate through a vector line by line looking for last element on each line, last(). The problem is, after appending all chars in a string and try to count clusters of values in that string, I get seg fault. I feel like my logic behind is ok, but I am not sure what I did wrong. Can someone help...
Hi, can I get an analysis of the case "Repeal of the Luxury tax" from the...
Hi, can I get an analysis of the case "Repeal of the Luxury tax" from the textbook "Business and its Environment" by David Baron.
Hi there! I can not find solution of Octave Levenspiel Chapter 26. I need it immediately....
Hi there! I can not find solution of Octave Levenspiel Chapter 26. I need it immediately. Can you please help me!Thank you. A solid feed consisting of 20 wt% of 1-mm particles and smaller 30 wt% of 2-mm particles 50 wt% of 4-mm particles passes through a rotating tubular reactor somewhat like a cement kiln where it reacts with gas to give a hard nonfriable solid product (SCMI reaction control, T = 4 h for 4-mm particles). 26.3. Find the...
HI , CAN I GET ANOTHE ANSWER BECAUSE THE ANSWER IS ALREADY TAKEN BY OTHER STUDEN...
HI , CAN I GET ANOTHE ANSWER BECAUSE THE ANSWER IS ALREADY TAKEN BY OTHER STUDEN IN MY CLASS,IS ABOUT MATERNITY CHILD NURSING CLASS THE QUESTION IS :How can electronic fetal monitoring inhibit the normal progress of labor? What can nurses do as advocates to counteract this effect?
Hi, can i get some explanation/clarification in this: 4. Elaborate on Ida Susser’s argument that the...
Hi, can i get some explanation/clarification in this: 4. Elaborate on Ida Susser’s argument that the concept of the United States itself has a history of shifting frontiers and contested boundaries. ( the article is about cultural Diversity in the United States and I can't seem to understand what it is asking me to do)
Hi, I need your answer this question below.Br/H a) A market has the following demand function:...
Hi, I need your answer this question below.Br/H a) A market has the following demand function: ?(?) = 100 − 5? where y is total sold quantity of the good on the market and ?(?) is the price for which it sells for. What is the price elasticity of demand at y=10? (4 points) b) True or false? A Nash equilibrium is always a subgame perfect Nash equilibrium. Explain! (3 points) c) Explain what third-degree price discrimination is and under...
Is there a shortcut or simpler way to get the same output in C ? #include...
Is there a shortcut or simpler way to get the same output in C ? #include <stdio.h> #include <time.h> #include <stdlib.h> #define OFFSET 1 #define RANGE 53 int RandomInteger(int, int); int PrintInstructions(); int main() { //six integer variables int a, b, c, d, e, f; //one integer for num of sets int numSets; srand(time(NULL)); while (1) { int i; numSets = PrintInstructions(); if (numSets == 0) { printf("* You have choosen to exit the application.\n"); break; } for (i =...
Hello I have these questions, Can I please get some assistance, I need not so long...
Hello I have these questions, Can I please get some assistance, I need not so long or short answers please How to decide what job to take. Is that a marginal decision? What is an explicit cost? What is an implicit cost? How is accounting profit calculated? How is economic profit calculated?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT