0

I have next array with strings:

['val1=123','val2=456']

How I can split it to object with params and values?

{
  val1: 123,
  val2: 456,
}
6
  • 3
    Have you tried split with map and/or reduce? Commented May 28, 2021 at 9:47
  • res.map(x => x.split('=')); returns me arrays Commented May 28, 2021 at 9:49
  • There are dozens of duplicates. What are you struggling with? This is pretty similar: stackoverflow.com/questions/42974735/… Commented May 28, 2021 at 9:50
  • @Ted save that output to a variable res2 or similar. Then you can use Object.fromEntries(res2) Commented May 28, 2021 at 9:51
  • const res = ['val1=123','val2=456']; res.map(x => x.split('=')); result: [ [ "val1", "123" ], [ "vall2", "456" ], Commented May 28, 2021 at 9:52

6 Answers 6

3

const recordStrings = ['val1=123', 'val2=456']
const record = Object.fromEntries(
  recordStrings.map(str => str.split('='))
)

console.log(record)

Explanation:

recordStrings.map(str => str.split('=')) returns [[val1, 123], [val2, 456]].

Object.fromEntries(entries) creates an object from an array containing [key, value] tuples.

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

1 Comment

Object.fromEntries that's amazing
2

You can try with reduce method, it's really helpful to convert an array to any others data type like an object, string, number.

const arr = ['val1=123','val2=456'];

const items = arr.reduce((total, item) => {
  const [key, value] = item.split('=');
  if (key) {
    total[key] = value
  }
  return total;
}, {})

console.log(items);

1 Comment

Array#reduce is very powerful, which is why it is poor at communicating intent. I would recommend a combination of Array#map and Object.fromEntries for clarity.
1

Split the strings in the array and convert the array to an object:

const res = ['val1=123','val2=456'];
const result = Object.fromEntries(res.map(x => {
    const [l, r] = x.split('=');
    return [l, +r];
}));
console.log(result);

1 Comment

Object.fromEntries that's amazing
1

let obj = {};
let arr = ['val1=123', 'val2=456'];

arr.forEach(i => {
  let x = i.split('=');
  obj[x[0]] = parseInt(x[1]);
});

console.log(obj);

Comments

1

let arr = ['val1=123', 'val2=456']
let object = {}

for (let i = 0; i < arr.length; i++) {
  var split = arr[i].split("=")
  object[split[0]] = split[1]
}

console.log(object); // { val1: '123', val2: '456' }

Comments

0

let arr = ['val1=123','val2=456'];
    arr.forEach(str => {
        let arrStr = str.split('=');
        eval(arrStr[0] + '= ' + arrStr[1] + ';');
    })
    console.log(val1, val2);

OR

let arr = ['val1=123','val2=456'];
    arr.forEach(str => {
        let arrStr = str.split('=');
        window[arrStr[0]] = arrStr[1];
    })
    console.log(val1, val2);

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.