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, 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 , 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, 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?
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their...
Hi i need a c++ program that can convert charactor into numbers pseodocode user input their name for example john every single charactor should have a value so it return correct result eg: j=2, o=1, h=7,n=2 and display these numbers if you may need any question to ask me, i will be very happy to answer them. Thanks! example project close but not accurate #include #include #include #include #include #include #include #include #define INPUT_SIZE 8 using namespace std; // Function...
I get the following errors when I type the code #include <iostream> #include <string.h> #include <bitset>...
I get the following errors when I type the code #include <iostream> #include <string.h> #include <bitset> #include <math.h> #define IS_INTEGRAL(T) typename std::enable_if< std::is_integral<T>::value >::type* = 0 using namespace std; template<class T> std::string inttobits1(T byte, IS_INTEGRAL(T)) { std::bitset<sizeof(T)* CHAR_BIT> bs(byte); return bs.to_string(); } int bitstoint1(string bits) { int value; for (int x = 0, y = bits.length() - 1;x < 8 && x < bits.length();x++) { int val = atoi(bits.substr(y, 1).c_str()); value += (int)val * pow(2, x); } return value; }...
Hi,I need the fastest possible answer, I need solution for this issue with all the details...
Hi,I need the fastest possible answer, I need solution for this issue with all the details just nu .BR/Ha Q. Answer whether the following examples of Foreign Direct Investment (FDI) describe horizontal or vertical FDI. Explain your answers. a) Volvo builds a car factory in Austin, Texas, which is similar to Volvo’s factory in Gothenburg. The main purpose is to avoid US import tariffs. b) H&M relocates production of its clothes from Sweden to Bangladesh to reduce wage costs. (5...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT