【问题标题】:group by multiple times into object using typescript使用打字稿多次分组到对象中
【发布时间】:2020-07-18 12:34:26
【问题描述】:

我很难找出如何按对象数组中的多个数据进行分组。我的第一个代码显示了我如何按我的第一个数据进行分组,即 formuleWazari。我想在这个小组之后再做一个。

我的代码:

const groupBy = (array, key) => {
  // Return the end result
  return array.reduce((result, currentValue) => {
    // If an array already present for key, push it to the array. Else create an array and push the object
    (result[formuleWazari(currentValue[key])] = result[formuleWazari(currentValue[key])]  || []).push(
      new wazari(currentValue['product'], currentValue['warranty'])
     // new wazariFinal(currentValue['warranty'])
    );

    // Return the current iteration `result` value, this will be taken as next iteration `result` value and accumulate
    return result;
  }, {}); // empty object is the initial value for result object

};


var  personGroupedByColor = groupBy(obj.warrantySettings, 'formula');

这段代码给了我这个结果:

{
  "Basic": [
    {
      "product": 3,
      "warranty": 1
    },
    {
      "product": 3,
      "warranty": 8
    },
    {
      "product": 3,
      "warranty": 6
    },
    {
      "product": 4,
      "warranty": 7
    },
    {
      "product": 4,
      "warranty": 14
    }
  ],
  "Confort": [
    {
      "product": 3,
      "warranty": 1
    },
    {
      "product": 3,
      "warranty": 8
    },
    {
      "product": 3,
      "warranty": 2
    },
    {
      "product": 3,
      "warranty": 3
    },
    {
      "product": 3,
      "warranty": 10
    },
    {
      "product": 3,
      "warranty": 11
    },
    {
      "product": 3,
      "warranty": 6
    },
    {
      "product": 4,
      "warranty": 7
    },
    {
      "product": 4,
      "warranty": 14
    }
  ],

}

我想再次按产品分组。我该怎么做才能让 json 看起来像这样(在此先感谢大家的帮助):

{
  "Basic": [

    { "product":[
                3 :{
                  "warranty": 1,
                  "warranty": 8
                },
                 4 :{
                  "warranty": 10,
                  "warranty": 12
                },

]
]}}

【问题讨论】:

  • 您能否更清楚地说明问题?您要按功能分组执行的操作,请清楚地添加保修设置等输入以及所需的输出。

标签: javascript arrays json typescript


【解决方案1】:

我找到了解决方案:


const groupBy = (array, key) => {
      // Return the end result
      return array.reduce((result, currentValue) => {
        // If an array already present for key, push it to the array. Else create an array and push the object
        (result[formuleWazari(currentValue[key])]  = result[formuleWazari(currentValue[key])]  || []).push(
      );
        (result[formuleWazari(currentValue[key])] [productWazari(currentValue['product'])]  = result[formuleWazari(currentValue[key])] [productWazari(currentValue['product'])]  || []).push(

          garantieWazari(currentValue['warranty'])
        );
        // Return the current iteration `result` value, this will be taken as next iteration `result` value and accumulate
        return result;
      }, {}); // empty object is the initial value for result object
    };


【讨论】:

    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 2019-07-10
    • 2021-01-26
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 2019-04-20
    • 2020-08-06
    相关资源
    最近更新 更多