сгруппировать несколько раз в объект, используя машинопись - PullRequest
0 голосов
/ 06 апреля 2020

У меня есть некоторые трудности, чтобы выяснить, как я могу сгруппировать по нескольким данным в массиве объектов. мой первый код показывает, как я сделал для группировки по моим первым данным, который является 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
                },

]
]}}

1 Ответ

0 голосов
/ 06 апреля 2020

Я нашел решение:


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


Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...