3

I have array of key and value pairs as

let array = [
  "Social Network: 1",
  "Tasks: 1",
  "Calendar: 1",
  "Journal: 1",
  "Business Contact Manager: 2"
];

I want convert this into an object as shown below:

{
  "Social Network": 1,
  "Tasks": 1,
  "Calendar": 1,
  "Journal": 1,
  "Business Contact Manager": 2
}

How can I achieve this?

2
  • It seems you meant that you want convert your array into a json object. Is that correct? Commented Mar 9, 2020 at 10:31
  • Iterate over the array, split every element over :, add a new key/value to the object according to these two parts Commented Mar 9, 2020 at 10:31

6 Answers 6

4

You can use .reduce() and .split() to get the desired output:

let array = ["Social Network: 1", "Tasks: 1", "Calendar: 1", "Journal: 1", "Business Contact Manager: 2"];

let result = array.reduce((r, c) => {
  let [k, v] = c.split(":");
  r[k] = Number(v);
  return r;
}, {});

console.log(result);

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

Comments

3

You could build a valid JSON and parse it.

var data = ["Social Network: 1", "Tasks: 1", "Calendar: 1", "Journal: 1", "Business Contact Manager: 2"];
    result = JSON.parse('{"' + data.join(',"').replace(/:\s/g, '":') + '}');
console.log(result);

Comments

1
let array = [
  "Social Network: 1",
  "Tasks: 1",
  "Calendar: 1",
  "Journal: 1",
  "Business Contact Manager: 2"
];

const desiredObj = array.reduce((acc, currentItem) => {
  let arr = currentItem.split(":");
  acc[arr[0]] = Number(arr[1]);
  return acc;
}, {});

console.log(desiredObj);

Comments

1

You could .map() each string to a split [key, value] array, where key is the portion to the left of the : and value is the number to the right of the :. You can then use Object.fromEntries() to build an object from your key-value pair arrays:

const array = ["Social Network: 1", "Tasks: 1", "Calendar: 1", "Journal: 1", "Business Contact Manager: 2"];

const res = Object.fromEntries(array.map(str => {
  const [a, b] = str.split(': ');
  return [a, +b];
}));
console.log(res);

Alternatively, if you can't support Object.fromEntries(), you can use Object.assign() instead by mapping to an object and then spreading the mapped objects into the arguments of .assign():

const array = ["Social Network: 1", "Tasks: 1", "Calendar: 1", "Journal: 1", "Business Contact Manager: 2"];

const res = Object.assign({}, ...array.map(str => {
  const [a, b] = str.split(': ');
  return {[a]: +b};
}));
console.log(res);

Comments

0

If performance is of concern, go with this "classic" for loop solution. It is the fastest solution and quite considerably (up to 30%).

let array = ["Social Network: 1", "Tasks: 1", "Calendar: 1", "Journal: 1", "Business Contact Manager: 2"];
let result = {};

for (let i = 0; i < array.length; i++) {
  let item = array[i];
  let pair = item.split(':');
  let key = pair[0];
  let value = parseFloat(pair[1]);
  result[key] = value;
}

console.log(result);

Comments

0

Array#reduce is perfect for this:

let arr = ["Social Network: 1", "Tasks: 1", "Calendar: 1", "Journal: 1", "Business Contact Manager: 2"]

const result = arr.reduce((acc, c, _, __, [k, v] = c.split(': ')) => 
    (acc[k] = Number(v), acc), {})

console.log(result)

Or how about this strange one:

let arr = ["Social Network: 1", "Tasks: 1", "Calendar: 1", "Journal: 1", "Business Contact Manager: 2"]

const result = Object.assign(...arr.map((el) =>
    Object.fromEntries([el.split(': ').map((v, i) => i%2 ? Number(v) : v)])))

console.log(result)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.