I get my response array with duplicate values. Id, Name and Cod, but there are itens differents like Indicator. So I grouped my array using reduce. This is my array:
let myArray = [  
  {  
    "id":1,
    "name":"NAME1",
    "cod":"00CC",
    "indicator":{  
      "idIndicator":2,      
      "name":"MY-SECOND-NAME",
    }
  },
  {  
    "id":1,
    "name":"NAME1",
    "cod":"00CC",
    "indicator":{  
      "idIndicator":3,
      "name":"MY-FIST-NAME",
    }
  },
  {  
    "id":5,
    "name":"DWWW",
    "cod":"200CC",
    "indicator":{  
      "idIndicator":2,
      "name":"MY-SECOND-NAME",
    }
  },
  {  
    "id":5,
    "name":"DWWW",
    "cod":"200CC",
    "indicator":{  
      "idIndicator":3,
      "name":"MY-FIST-NAME",
    }
  }
]
console.log("My Array with duplicate values:" )
console.log(myArray)
    var group_to_values = myArray.reduce(function (obj, item) {
      obj[item.cod] = obj[item.cod] || [];
      obj[item.cod].push(item.indicator);
      return obj;
    }, {});
    var groups = Object.keys(group_to_values).map(function (key) {
      return {
        cod: key,
        indicatorItem: group_to_values[key]
      };
    });
console.log("My Array grouped:" )
console.log(groups)However, since reduce uses only 1 item (key) I lose the other values as: Id, Name
So, I tried to use a .map() and .filter() to find for items like id andname using cod as a reference, but I did not get much success. I'm going somewhere, but I do not know where :/
As I'm comparing item by item, the end of my return will always be the last item (cod) in my array. I understand that so the return is only the cod 00CC
This is my code:
let returnedItens = null
myArray.map(itemArray => {
  returnedItens = groups.filter(itemGroup => {
      if (itemArray.cod == itemGroup.cod) {
        return {
          id: itemArray.id,
          name: itemArray.name,
          cod: itemGroup.cod,
          indicator: itemGroup.indicator,
        }
      }
    })
  })
So, I would like to get this return with my code above:
[  
  {  
    "id":1,
    "name":"NAME1",
    "cod":"00CC",
    "indicator":[  
      {  
        "idIndicator":2,
        "name":"MY-SECOND-NAME",
      },
      {  
        "idIndicator":3,
        "name":"MY-FIST-NAME",
      }
    ]
  },
  {  
    "id":5,
    "name":"DWWW",
    "cod":"200CC",
    "indicator":[  
      {  
        "idIndicator":2,
        "name":"MY-SECOND-NAME",
      },
      {  
        "idIndicator":3,
        "name":"MY-FIST-NAME",
      }
    ]
  }
]
Please, someone can help me? How I can do it?
 
     
     
     
    