简体   繁体   English

如何在将相同 ID 分组的地方重组数据?

[英]How can I restructure the data where it would group the same IDs?

I have this data where it repeats the items that were placed.我有这个数据,它重复放置的项目。 How can I group the data according to their id and add an array of variations that will store the various colors and their quantity?如何根据它们的 id 对数据进行分组并添加一组变量来存储各种颜色及其数量?

This is the current sample data that I have:这是我拥有的当前样本数据:

 const data = [
      {
        id: "aRLMZkiSU7T0lcsPCSsV",
        name: "Tumbler",
        price: 200,
        size: "500",
        cat: "ML",
        color: "Green",
        quantity: 2
      },
      {
        id: "aRLMZkiSU7T0lcsPCSsV",
        name: "Tumbler",
        price: 200,
        size: "500",
        cat: "ML",
        color: "Pink",
        quantity: 1
      },
      {
        id: "aRLMZkiSU7T0lcsPCSsV",
        name: "Tumbler",
        price: 200,
        size: "500",
        cat: "ML",
        color: "Black",
        quantity: 11
      },
      {
        id: "y9ECyZBKp2OBekmWym4M",
        name: "Notebook",
        price: 250,
        size: "200",
        cat: "CM",
        color: "Red",
        quantity: 1
      },
      {
        id: "y9ECyZBKp2OBekmWym4M",
        name: "Notebook",
        price: 250,
        size: "200",
        cat: "CM",
        color: "Green",
        quantity: 4
      }
    ];

I just want to know how I could destructure this data to something like this where the duplicated id will be group and then the color and quantity will be pushed inside the variations array:我只想知道如何将这些数据解构为这样的东西,其中重复的 id 将被分组,然后颜色和数量将被推送到变量数组中:

   id: "aRLMZkiSU7T0lcsPCSsV",
    name: "Tumbler",
    price: 200,
    size: "500",
    cat: "ML",
    variations: [
       {name: "Green", quantity: 1},
       {name: "Black", quantity: 10},
    ]

From what I have done, I have merged it already except for the part for the variations array.根据我所做的,我已经合并了它,除了变量数组的部分。 How can I create the variations array ?如何创建variations数组?

codesandbox: https://codesandbox.io/s/data-restructure-u2ss8z?file=/src/App.js代码沙盒: https ://codesandbox.io/s/data-restructure-u2ss8z?file=/src/App.js

const mapById = data.reduce(
  (acc, { id, name, size, cat, price, color, quantity }) => {
    acc[id] = {
      id,
      name,
      size,
      cat,
      price,
      color,
      quantity
    };
    return acc;
  },
  {}
);

Initially you have to check if an object with id key is present, if not create it with an empty variations array.最初,您必须检查是否存在具有id键的对象,如果不存在则使用空的variations数组创建它。 then added to the variations array based on the current id然后根据当前id添加到变体数组中

 const data = [ { id: "aRLMZkiSU7T0lcsPCSsV", name: "Tumbler", price: 200, size: "500", cat: "ML", color: "Green", quantity: 2 }, { id: "aRLMZkiSU7T0lcsPCSsV", name: "Tumbler", price: 200, size: "500", cat: "ML", color: "Pink", quantity: 1 }, { id: "aRLMZkiSU7T0lcsPCSsV", name: "Tumbler", price: 200, size: "500", cat: "ML", color: "Black", quantity: 11 }, { id: "y9ECyZBKp2OBekmWym4M", name: "Notebook", price: 250, size: "200", cat: "CM", color: "Red", quantity: 1 }, { id: "y9ECyZBKp2OBekmWym4M", name: "Notebook", price: 250, size: "200", cat: "CM", color: "Green", quantity: 4 } ]; const mapById = data.reduce( (acc, { id, name, size, cat, price, color, quantity }) => { acc[id] ??= {id,name,size,cat,price,variations:[]} acc[id].variations.push({name:color, quantity}) return acc; }, {} ); console.log(mapById)

Presented below code-snippet may be helpful to achieve the objective.下面介绍的代码片段可能有助于实现目标。 Please note that this returns an Array and not a map.请注意,这将返回一个Array而不是地图。 However, if one removes the Object.values wrapper, then the result will be a map/object with id being the key.但是,如果删除Object.values包装器,则结果将是一个以id为键的映射/对象。

 const regroupArray = arr => ( Object.values( arr.reduce( (acc, {id, color, quantity, ...rest}) => ( id in acc ? { ...acc, [id]: { ...acc[id], variations: acc[id].variations.concat([{ color, quantity }]) } } : { ...acc, [id]: { id, ...rest, variations: [{ color, quantity }] } } ), {} ) ) ); const data = [ { id: "aRLMZkiSU7T0lcsPCSsV", name: "Tumbler", price: 200, size: "500", cat: "ML", color: "Green", quantity: 2 }, { id: "aRLMZkiSU7T0lcsPCSsV", name: "Tumbler", price: 200, size: "500", cat: "ML", color: "Pink", quantity: 1 }, { id: "aRLMZkiSU7T0lcsPCSsV", name: "Tumbler", price: 200, size: "500", cat: "ML", color: "Black", quantity: 11 }, { id: "y9ECyZBKp2OBekmWym4M", name: "Notebook", price: 250, size: "200", cat: "CM", color: "Red", quantity: 1 }, { id: "y9ECyZBKp2OBekmWym4M", name: "Notebook", price: 250, size: "200", cat: "CM", color: "Green", quantity: 4 } ]; console.log(regroupArray(data));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM