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,
}
Ted
  • 1,682
  • 3
  • 25
  • 52
  • 3
    Have you tried [`split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) with [`map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) and/or [`reduce`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)? –  May 28 '21 at 09:47
  • res.map(x => x.split('=')); returns me arrays – Ted May 28 '21 at 09:49
  • There are dozens of duplicates. What are you struggling with? This is pretty similar: https://stackoverflow.com/questions/42974735/create-object-from-array#42974762 –  May 28 '21 at 09:50
  • @Ted save that output to a variable `res2` or similar. Then you can use `Object.fromEntries(res2)` – evolutionxbox May 28 '21 at 09:51
  • const res = ['val1=123','val2=456']; res.map(x => x.split('=')); result: [ [ "val1", "123" ], [ "vall2", "456" ], – Ted May 28 '21 at 09:52
  • @Ted please do not post code in the comments. – evolutionxbox May 28 '21 at 09:53

6 Answers6

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.

lpf
  • 73
  • 1
  • 6
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);
Mamunur Rashid
  • 1,095
  • 17
  • 28
  • `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. – lpf Jun 02 '21 at 12:06
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' }
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);
AbsoluteBeginner
  • 2,160
  • 3
  • 11
  • 21
Vivek Bani
  • 3,703
  • 1
  • 9
  • 18
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);
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);