1

I get a response from a server like this:

3S1,https://lekcjaplus.vulcan.net.pl
TA1,https://uonetplus-komunikacja.umt.tarnow.pl
OP1,https://uonetplus-komunikacja.eszkola.opolskie.pl
RZ1,https://uonetplus-komunikacja.resman.pl
GD1,https://uonetplus-komunikacja.edu.gdansk.pl
P03,https://efeb-komunikacja-pro-efebmobile.pro.vulcan.pl
P01,http://efeb-komunikacja.pro-hudson.win.vulcan.pl
P02,http://efeb-komunikacja.pro-hudsonrc.win.vulcan.pl
P90,http://efeb-komunikacja-pro-mwujakowska.neo.win.vulcan.pl

I want to convert it to an object like this:

"3S1": "https://lekcjaplus.vulcan.net.pl",
"TA1": "https://uonetplus-komunikacja.umt.tarnow.pl",
"OP1": "https://uonetplus-komunikacja.eszkola.opolskie.pl",
"RZ1": "https://uonetplus-komunikacja.resman.pl",
"GD1": "https://uonetplus-komunikacja.edu.gdansk.pl",
"P03": "https://efeb-komunikacja-pro-efebmobile.pro.vulcan.pl",
"P01": "http://efeb-komunikacja.pro-hudson.win.vulcan.pl",
"P02": "http://efeb-komunikacja.pro-hudsonrc.win.vulcan.pl",
"P90": "http://efeb-komunikacja-pro-mwujakowska.neo.win.vulcan.pl"

What's the simplest way to achieve this?

3
  • 1
    Is the top one a single string or a series of strings? Commented May 10, 2018 at 12:27
  • do you get an array of strings or one of line in top??? Commented May 10, 2018 at 12:28
  • It's simply one, multi-line string. Commented May 10, 2018 at 12:32

2 Answers 2

5

You can split by new line and use reduce

let str = `3S1,https://lekcjaplus.vulcan.net.pl
	TA1,https://uonetplus-komunikacja.umt.tarnow.pl
	OP1,https://uonetplus-komunikacja.eszkola.opolskie.pl
	RZ1,https://uonetplus-komunikacja.resman.pl
	GD1,https://uonetplus-komunikacja.edu.gdansk.pl
	P03,https://efeb-komunikacja-pro-efebmobile.pro.vulcan.pl
	P01,http://efeb-komunikacja.pro-hudson.win.vulcan.pl
	P02,http://efeb-komunikacja.pro-hudsonrc.win.vulcan.pl
	P90,http://efeb-komunikacja-pro-mwujakowska.neo.win.vulcan.pl`;

let result = str.split(/\n/).reduce((c, v) => {
  if( v.trim() !== '' ) {
       let [k, o] = v.trim().split(',');
       c[k] = o;
  }
  return c;
}, {});

console.log(result);

In case you have multiple , on each line, you can deconstruct the array and join(',')

let result = str.split(/\n/).reduce((c,v)=>{
    if( v.trim() ) {
        let [k,...o] = v.trim().split(',');
        c[k] = o.join(',');
    }
    return c;
},{});
Sign up to request clarification or add additional context in comments.

Comments

0

First of all, you need to split the string into lines

Then split every line into two parts, the first part will be a key and the second one will be the value of the same key.

let input = `3S1,https://lekcjaplus.vulcan.net.pl
TA1,https://uonetplus-komunikacja.umt.tarnow.pl
OP1,https://uonetplus-komunikacja.eszkola.opolskie.pl
RZ1,https://uonetplus-komunikacja.resman.pl
GD1,https://uonetplus-komunikacja.edu.gdansk.pl
P03,https://efeb-komunikacja-pro-efebmobile.pro.vulcan.pl
P01,http://efeb-komunikacja.pro-hudson.win.vulcan.pl
P02,http://efeb-komunikacja.pro-hudsonrc.win.vulcan.pl
P90,http://efeb-komunikacja-pro-mwujakowska.neo.win.vulcan.pl`;


output = input.split(/\n/g);

output = output.reduce((acc, item) => {
    item = item.split(",");
    acc[item[0]] = item[1];
    return acc;
}, {})

console.log(output);

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.