按其不同的ID分组项目数组

时间:2021-05-21 21:25:55

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" }]
];

2 个解决方案

#1


2  

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);

#2


2  

What you want to do is group your items into 3 buckets.

你想要做的是将你的物品分成3个桶。

Based on the data, each bucket is indexed by shirt_id - 1.

根据数据,每个桶都由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.

我们的想法是迭代每个项目,根据当前衬衫的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);

#1


2  

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);

#2


2  

What you want to do is group your items into 3 buckets.

你想要做的是将你的物品分成3个桶。

Based on the data, each bucket is indexed by shirt_id - 1.

根据数据,每个桶都由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.

我们的想法是迭代每个项目,根据当前衬衫的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);