Reputation: 219
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?
Upvotes: 3
Views: 523
Reputation: 55729
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)
Upvotes: 0
Reputation: 50749
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);
Upvotes: 1
Reputation: 7488
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);
Upvotes: 0
Reputation: 386578
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);
Upvotes: 3
Reputation: 1508
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);
Upvotes: 1
Reputation: 39322
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);
Upvotes: 4