Transforming a data structure into javascript

Asked

Viewed 154 times

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’d like to turn 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 didn’t work very well the adaptations I made, someone knows an easy way to do it?

2 answers

0


You can unify the items by name into an object, and then turn them into the array of objects you need by traversing 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);

As there can be duplicate keys in objects, the line:

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

does the job of unifying the data for you. So a repeat loop on the unified object is enough to assemble the collection you need:

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

0

First order by chave name with the function Sort, so that the elements are all in ascending order. Make a for and insert 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.