1

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.

1
  • 1
    Expected output is an invalid javascript output format. Perhaps you wanted a key value pair object? Commented Aug 28, 2019 at 8:30

5 Answers 5

5

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

Sign up to request clarification or add additional context in comments.

2 Comments

It was helpful but my raw data doesn't have any '
@Chris that's irrelevant, it's just a string declaration ;)
4

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

Comments

1

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)

Comments

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

Comments

1

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

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.