Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
How to group array with multiple objects in javascript

I have an array

{
"id": 5308,
"empId": 202,
"loc": 1,
"salMonth": "1",
"salYear": "2020",
"salDate": "2020-01",
"netSal": 50000
},
{
"id": 5309,
"empId": 173,
"loc": 1,
"salMonth": "1",
"salYear": "2020",
"salDate": "2020-01",
"netSal": 30000
},
{
"id": 5310,
"empId": 212,
"loc": 1,
"salMonth": "1",
"salYear": "2020",
"salDate": "2020-01",
"netSal": 21100
},
{
"id": 5311,
"empId": 163,
"loc": 1,
"salMonth": "1",
"salYear": "2020",
"salDate": "2020-01",
"netSal": 43000
},
{
"id": 5312,
"empId": 116,
"loc": 1,
"salMonth": "1",
"salYear": "2020",
"salDate": "2020-01",
"netSal": 52000
},
{
"id": 5313,
"empId": 223,
"loc": 1,
"salMonth": "1",
"salYear": "2020",
"salDate": "2020-01",
"netSal": 21100
},
{
"id": 5314,
"empId": 231,
"loc": 1,
"salMonth": "1",
"salYear": "2020",
"salDate": "2020-01",
"netSal": 42217
}

I have to group this array by two colums, that is empId, salDate.

And the expected result should like:

{
  "202": {
    "2020-01": {
      "salId": xx,
      "salary": xxx
    },
    "2020-02": {
      "salId": xxx,
      "salary": xxxx
    },
    "2020-03": {
      "salId": xxx,
      "salary": xxxx
    }
  },
  "203": {
    "2020-01": {
      "salId": xx,
      "salary": xxx
    },
    "2020-02": {
      "salId": xxx,
      "salary": xxxx
    },
    "2020-03": {
      "salId": xxx,
      "salary": xxxx
    }
  },

}

Thanks Advance ............................................................................................................................................................................................................................................................................

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

A simple loop could solve your problem. try this.

let modifiedData = {}
let data = [{
    "id": 5308,
    "empId": 202,
    "loc": 1,
    "salMonth": "1",
    "salYear": "2020",
    "salDate": "2020-01",
    "netSal": 50000
  },
  {
    "id": 5309,
    "empId": 173,
    "loc": 1,
    "salMonth": "1",
    "salYear": "2020",
    "salDate": "2020-01",
    "netSal": 30000
  },
  {
    "id": 5310,
    "empId": 212,
    "loc": 1,
    "salMonth": "1",
    "salYear": "2020",
    "salDate": "2020-01",
    "netSal": 21100
  }
];

//loop your data and create modifed data
data.map(d => {
  if(!modifiedData[d.empId]) { // check weather empId already exists or not
     modifiedData[d.empId] = []
  }
  modifiedData[d.empId].push({[d.salDate]:{salId: d.id, salary: d.netSal}})
})
console.log(modifiedData) // print final data

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda