Reputation: 549
How do I go about re-arranging my array to by organized by shirt size:
[
{ shirt_id: 1, size: "small" },
{ shirt_id: 1, size: "medium" },
{ shirt_id: 1, size: "large" },
{ shirt_id: 2, size: "medium" },
{ shirt_id: 3, size: "large" }
];
Desired output:
[
[1, { size: "small" }, { size: "medium" }, { size: "large" }],
[2, { size: "medium" }],
[3, { size: "large" }]
];
Upvotes: 6
Views: 149
Reputation: 5339
try this:
let data = [{ shirt_id: 1, size: 'small' }, { shirt_id: 1, size: 'small' },
{ shirt_id: 1, size: 'medium' },
{ shirt_id: 1, size: 'large' },
{ shirt_id: 2, size: 'medium' },
{ shirt_id: 3, size: 'large' }
];
let result = data.reduce(function(result, obj) {
let idPos = result.map(v => v[0]).indexOf(obj.shirt_id);
if (idPos > -1) {
let sizeArr = result[idPos].slice(1).map(obj => obj.size);
if (sizeArr.indexOf(obj.size) < 0) {
result[idPos].push({ 'size': obj.size });
}
} else {
result.push([obj.shirt_id, { 'size': obj.size }]);
}
return result;
}, []);
console.log(result);
Upvotes: 2
Reputation: 35491
What you want to do is group your items into 3 buckets.
Based on the data, each bucket is indexed by shirt_id - 1
.
The idea is to iterate through each item, filling the appropriate bucket with the shirt size based on the current shirt's id.
const data=[{shirt_id:1,size:"small"},{shirt_id:1,size:"medium"},{shirt_id:1,size:"large"},{shirt_id:2,size:"medium"},{shirt_id:3,size:"large"}];
const getBucketNumFromShirtId = shirtId => shirtId - 1;
const result = data.reduce((buckets, item) => {
// determine bucket index from the shirt id
const bucketNum = getBucketNumFromShirtId(item.shirt_id);
// if the bucket corresponding to the bucket num doesn't exist
// create it and add the shirt id as the first item
if (!Array.isArray(buckets[bucketNum])) {
buckets[bucketNum] = [item.shirt_id];
}
// add the shirt size into the appropriate bucket
buckets[bucketNum].push({ size: item.size });
// return buckets to continue the process
return buckets;
}, []);
console.log(result);
Upvotes: 2