Chris
Chris

Reputation: 1304

Make array of String as Key Value pair

I am getting a raw data like this

SECRET_TOKEN=Iwillruletheworld;SECRET_REFRESH_TOKEN=Iwillruletheworld;SERVER_PORT=3000;SERVER_WS_PORT=4000;NODE_URI=http://test.abc.com/#/;MONGODB_DEFAULT_URI=mongodb;MONGODB_HOST=localhost;MONGODB_PORT=27017;

I want to make this as a key value pair. So, first I separated the data by ; like this

data.split(';');

Output

[ 'SECRET_TOKEN=Iwillruletheworld',
  'SECRET_REFRESH_TOKEN=Iwillruletheworld',
  'SERVER_PORT=3000',
  'SERVER_WS_PORT=4000',
  'NODE_URI=http://test.abc.com/#/',
  'MONGODB_DEFAULT_URI=mongodb',
  'MONGODB_HOST=localhost',
  'MONGODB_PORT=27017'
]

Now I want to make it as key value pair

Expected Output

[ 'SECRET_TOKEN'='Iwillruletheworld',
  'SECRET_REFRESH_TOKEN'='Iwillruletheworld',
  'SERVER_PORT'='3000',
  'SERVER_WS_PORT'='4000',
  'NODE_URI'='http://test.abc.com/#/',
  'MONGODB_DEFAULT_URI'='mongodb',
  'MONGODB_HOST'='localhost',
  'MONGODB_PORT'='27017'
]

I want to insert ' wherever = occurs. Can anyone please help me out.

Upvotes: 1

Views: 4473

Answers (5)

briosheje
briosheje

Reputation: 7446

Assuming you want an object as a result instead (since the output you provided is actually invalid), you could split by ;, remove empty items (.filter(Boolean)) and reduce to build up a key-value pair object.

Of course, this example assumes there are no duplicate keys in the input.

let input = `SECRET_TOKEN=Iwillruletheworld;SECRET_REFRESH_TOKEN=Iwillruletheworld;SERVER_PORT=3000;SERVER_WS_PORT=4000;NODE_URI=http://test.abc.com/#/;MONGODB_DEFAULT_URI=mongodb;MONGODB_HOST=localhost;MONGODB_PORT=27017;`;

let res = input.split(';').filter(Boolean).reduce((acc, next) => {
  let [key, value] = next.split('=');
  acc[key] = value;
  return acc;
}, {});

console.log(res);

Edit (2021): another more "modern" solution could also be using Object.fromEntries instead of .reduce. The result is exactly the same, it's just another similar approach.

let input = `SECRET_TOKEN=Iwillruletheworld;SECRET_REFRESH_TOKEN=Iwillruletheworld;SERVER_PORT=3000;SERVER_WS_PORT=4000;NODE_URI=http://test.abc.com/#/;MONGODB_DEFAULT_URI=mongodb;MONGODB_HOST=localhost;MONGODB_PORT=27017;`;

const mapEntries = Object.fromEntries(
  input.split(';').filter(Boolean).map(v => v.split('='))
);
console.log(mapEntries);

Upvotes: 5

StepUp
StepUp

Reputation: 38114

Try this one to have an array of strings:

const str = 'SECRET_TOKEN=Iwillruletheworld;SECRET_REFRESH_TOKEN=Iwillruletheworld;SERVER_PORT=3000;SERVER_WS_PORT=4000;NODE_URI=http://test.abc.com/#/;MONGODB_DEFAULT_URI=mongodb;MONGODB_HOST=localhost;MONGODB_PORT=27017;'
const strArray = str.split(';');
const corrArray = strArray.map(s=> s.replace('=', "'='")).map(s=> `'${s}'`);
console.log(corrArray);

If you want objects:

const str = 'SECRET_TOKEN=Iwillruletheworld;SECRET_REFRESH_TOKEN=Iwillruletheworld;SERVER_PORT=3000;SERVER_WS_PORT=4000;NODE_URI=http://test.abc.com/#/;MONGODB_DEFAULT_URI=mongodb;MONGODB_HOST=localhost;MONGODB_PORT=27017;'
const objs = str.split(';').map(a=> {
    const divided = a.split('=');
    const obj = {};
    obj[divided[0]] = divided[1];
    return obj;
})

Upvotes: 1

Anmol Tiwari
Anmol Tiwari

Reputation: 89

let data = 'SECRET_TOKEN=Iwillruletheworld;SECRET_REFRESH_TOKEN=Iwillruletheworld;SERVER_PORT=3000;SERVER_WS_PORT=4000;NODE_URI=http://test.abc.com/#/;MONGODB_DEFAULT_URI=mongodb;MONGODB_HOST=localhost;MONGODB_PORT=27017';
//assuming the data you receive is a string

let arr = data.split(';');
let dataObj = {};
for (let piece of arr){
   let pieceToKeyVal = piece.split('=');
   dataObj[pieceToKeyVal[0]] = pieceToKeyVal[1];
}
console.log(dataObj);

Upvotes: 1

arizafar
arizafar

Reputation: 3122

You can split with ; then split each with = and return array of object using Array.prototype.map then use Object.assign to convert it into object

let str = 'SECRET_TOKEN=Iwillruletheworld;SECRET_REFRESH_TOKEN=Iwillruletheworld;SERVER_PORT=3000;SERVER_WS_PORT=4000;NODE_URI=http://test.abc.com/#/;MONGODB_DEFAULT_URI=mongodb;MONGODB_HOST=localhost;MONGODB_PORT=27017';
let out = Object.assign(str.split(';').map(e => ({[e.split('=')[0]]:e.split('=')[1]})));
console.log(out)

Upvotes: 1

Djaouad
Djaouad

Reputation: 22776

You can turn it to a JavaScript object (not an array):

var arr = [ 'SECRET_TOKEN=Iwillruletheworld',
  'SECRET_REFRESH_TOKEN=Iwillruletheworld',
  'SERVER_PORT=3000',
  'SERVER_WS_PORT=4000',
  'NODE_URI=http://test.abc.com/#/',
  'MONGODB_DEFAULT_URI=mongodb',
  'MONGODB_HOST=localhost',
  'MONGODB_PORT=27017'
];

var obj = {};

arr.forEach((x) => {
  var kv = x.split('=');
  obj[kv[0]] = kv[1];
});

console.log(obj);

Upvotes: 4

Related Questions