Reputation: 1094
Given the following JSON
{
gif: ['small', 'medium', 'large'],
jpeg: ['tiny', 'huge']
}
I would like to convert each element in the array into a key in a sub object and assign it to it a quantity of 1.
As such
{
gif: {
small: {quantity: 1},
medium: {quantity: 1},
large: {quantity:1}
},
jpeg: {
tiny: {quantity:1},
huge: {quantity: 1}
}
}
The closest I've come is with the following
for(let image in obj) {
obj[image].forEach(size => { obj[image][size] = { quantity:1}})
}
However unfortunately this out puts
{ gif:
[ 'small',
'medium',
'large',
small: {quantity: 1},
medium: {quantity: 1},
large: {quantity: 1} ],
jpeg: [ 'huge', 'tiny', tiny: {quantity:1} huge: {quantity: 1 } ]
}
Any help would be awesome, thanks
Upvotes: 3
Views: 73
Reputation: 1091
You can use the .reduce()
method
const obj = {
gif: ['small', 'medium', 'large'],
jpeg: ['tiny', 'huge']
};
for (let image in obj) {
obj[image] = obj[image].reduce((result, item) => {
result[item] = {
quantity: 1
};
return result;
}, {});
}
console.log(obj);
Upvotes: 1
Reputation: 18444
You can convert it in-place using reduce
within forEach
loop:
const o = {
gif: ['small', 'medium', 'large'],
jpeg: ['tiny', 'huge']
}
Object.keys(o)
.forEach(
k => o[k] = o[k].reduce(
(a, e) => (a[e] = {
quantity: 1
}) && a, {}
)
)
console.log(o)
And convert into a new object using double reduce
:
const o = {
gif: ['small', 'medium', 'large'],
jpeg: ['tiny', 'huge']
}
var o2 = Object.keys(o).reduce((n, k) => (n[k] = o[k].reduce((a, e) => (a[e] = {quantity:1}) && a, {})) && n, {})
console.log(o2)
Upvotes: 4
Reputation: 1197
const obj = {
gif: ['small', 'medium', 'large'],
jpeg: ['tiny', 'huge']
}
const objKeys = Object.keys(obj);
for(let i=0;i<objKeys.length;i++){
const currentKey = objKeys[i];
obj[currentKey] = obj[currentKey].reduce((sum, element) => {
sum[element] = {quantity: 1};
return sum;
}, {});
}
This is one solution, you go through the array and replace it with objects
Upvotes: 0
Reputation: 122145
You can use reduce
method on Object.keys
and inside you can use map
and Object.assign
to return new object.
const data = {
gif: ['small', 'medium', 'large'],
jpeg: ['tiny', 'huge']
}
const result = Object.keys(data).reduce(function(r, e) {
r[e] = Object.assign({}, ...data[e].map(k => ({[k]: {quantity: 1}})))
return r;
}, {})
console.log(result)
Upvotes: 0