Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group array of items by their distinct id

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" }]
];
like image 276
Yamoshi Wolverine Avatar asked Feb 06 '18 03:02

Yamoshi Wolverine


2 Answers

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);
like image 150
xianshenglu Avatar answered Oct 31 '22 03:10

xianshenglu


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);
like image 25
nem035 Avatar answered Oct 31 '22 03:10

nem035