How to group and filter fields with JS

1

I'm trying to get the following result using lodash:

[
 {
   "idpai": 1,
   "pai": "joao",
   "mae": "maria",
   "filhos": [
     {"id": 1, "nome": "joaozinho"},
     {"id": 2, "nome": "pedrinho"}
   ]
 }
]

with the following code:

var lista = [
  {idpai: 1, pai: 'joao', mae: 'maria', idfilho: 1, filho: 'marcos'},
  {idpai: 1, pai: 'joao', mae: 'maria', idfilho: 2, filho: 'joao'},
]

result = _.groupBy(lista, 'idpai')

result:

{
   "1": [
      {
         "idpai": 1,
         "pai": "joao",
         "mae": "maria",
         "idfilho": 1,
         "filho": "marcos"
      },
      {
         "idpai": 1,
         "pai": "joao",
         "mae": "maria",
         "idfilho": 2,
         "filho": "joao"
      }
   ]
}

As I'm using lodash I'm looking for something that can help me, my list comes duplicated because it's a relational database, is there any other way to solve it?

    
asked by anonymous 20.09.2018 / 21:34

2 answers

1

Hello,

Friend I believe this structure should come from your backend .

I think there is nothing more practical than doing with your own hands, I did something, a bit of gambiarra, but it returns to its structure:

link

    
20.09.2018 / 22:27
0

Friend uses alasql link: link

It will allow you to work as a sql in your javascript array.

I hope to have helped my friend.

    
20.09.2018 / 22:00