lnl
lnl

Reputation: 367

Splitting string to object in JS

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?

Upvotes: 1

Views: 119

Answers (2)

Mohamed Abbas
Mohamed Abbas

Reputation: 2278

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);

Upvotes: 0

Eddie
Eddie

Reputation: 26844

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;
},{});

Upvotes: 5

Related Questions