Transform a data structure into javascript

0

Hello,

I have the following data structure:

[
{id:1,idforeign:2,nome:'etc'},
{id:2,idforeign:2,nome:'etc'},
{id:3,idforeign:3,nome:'outro'},
{id:4,idforeign:3,nome:'outro'}
]

I would like to transform to something like this:

[
{nome:'etc', dados:[
        {id:1,idforeign:2},
        {id:2,idforeign:2}
    ]
},
{nome:'outro', dados:[
        {id:3,idforeign:3},
        {id:4,idforeign:3}
    ]
}
]

I've tried something like this: Group and add array in JavaScript

But it did not work very well the adaptations I made, does anyone know an easy way to do this?

    
asked by anonymous 30.09.2016 / 02:01

2 answers

0

You can unify the items by name in an object, and then transform them into the array of objects that need to traverse this unified object:

var input = [
  {id:1, idforeign:2, nome:'etc'},
  {id:2, idforeign:2, nome:'etc'},
  {id:3, idforeign:3, nome:'outro'},
  {id:4, idforeign:3, nome:'outro'}
];

var itens = {};

input.forEach(function(v, i) {
  if ("undefined" === typeof itens[v.nome]) {
    itens[v.nome] = []
  }
  itens[v.nome].push({id: v.id, idforeign: v.idforeign});
});

var output = [];

for (var item in itens) {
  output.push({nome: item, dados: itens[item]});
}

console.log(output);

Since there can be no duplicate keys in objects, the line:

itens[v.nome].push({id: v.id, idforeign: v.idforeign});

It does the job of unifying the data for you. So a loop of repetition in the unified object is enough to mount the collection you need:

for (var item in itens) {
  output.push({nome: item, dados: itens[item]});
}
    
30.09.2016 / 02:33
0

First sort by chave name with sort , so that the elements are all in ascending order. Make a for and enter in sequence the elements for chave .

var items = [
 {id: 1,idforeign: 2,nome: 'etc'}, 
 {id:2,idforeign:2,nome:'etc'}, 
 {id:3,idforeign:3,nome:'outro'}, 
 {id:4,idforeign:3,nome: 'outro'}
];

items.sort(function(a,b){
    var x = a.nome.toLowerCase();
    var y = b.nome.toLowerCase();
    return x < y ? -1 : x > y ? 1 : 0;
});

var key = '';
var pos = 0;
var newArray = [];
for (i = 0; i < items.length; i++) {
  if (key === '') {
    key = items[i].nome;
    newArray.push({
      'nome': items[i].nome,
      dados: [{
        'id': items[i].id,
        'idforeign': items[i].idforeign
      }]
    });
  } else {
    if (key === items[i].nome) {
      newArray[pos].dados
        .push({
          'id': items[i].id,
          'idforeign': items[i].idforeign
        });
    } else {
      key = items[i].nome;
      newArray.push({
        'nome': items[i].nome,
        dados: [{
          'id': items[i].id,
          'idforeign': items[i].idforeign
        }]
      });
      pos++;
    }
  }
}

console.log(newArray);

Code:

var items = [
 {id:1,idforeign:2,nome:'etc'}, 
 {id: 2,idforeign:2,nome: 'etc'}, 
 {id: 3,idforeign:3,nome: 'outro'}, 
 {id: 4,idforeign:3,nome: 'outro'}
];

items.sort(function(a,b){
   var x = a.nome.toLowerCase();
   var y = b.nome.toLowerCase();
   return x < y ? -1 : x > y ? 1 : 0;
});

var key = '';
var pos = 0;
var newArray = [];
for (i = 0; i < items.length; i++) {
  if (key === '') {
    key = items[i].nome;
    newArray.push({
      'nome': items[i].nome,
      dados: [{'id': items[i].id,'idforeign': items[i].idforeign
      }]
    });
  } else {
    if (key === items[i].nome) {
      newArray[pos].dados
        .push({'id': items[i].id,'idforeign': items[i].idforeign
        });
    } else {
      key = items[i].nome;
      newArray.push({
        'nome': items[i].nome,
        dados: [{'id': items[i].id,'idforeign': items[i].idforeign
        }]
      });
      pos++;
    }
  }
}

console.log(newArray);
    
30.09.2016 / 04:02