JS - Object array for array arrays

Asked

Viewed 2,700 times

7

How do I convert an array of objects to an array of arrays in javascript? I did so, but returns an array of objects

var items1 = new Array();


 $.map(objResposta, function(value,index) {
       teste = new Array();
       items1.push(value);
       teste.push(items1);

 });

inserir a descrição da imagem aqui

I need the values that are in red to stay in the array this way:

Array [ ['resposta atualizada', 5], ['esse foi', 4] ]

Is there any way popular like this?

  • You want for a generic case or just for that specific array?

3 answers

10


You already have an array... an array with objects, you have to convert into an array of arrays.

You can do it like this:

const array = [{
    nomePergunta: 'resposta atualizada',
    quantidadeResposta: 5
}, {
    nomePergunta: 'esse foi',
    quantidadeResposta: 4
}];

const novaArray = array.map(el => Object.keys(el).map(key => el[key]));

console.log(JSON.stringify(novaArray));

The idea is:

  • takes the initial array
  • converts each object into an array of its keys/properties
  • maps this array of each object in the values of these keys

Like the Mr. Felix referred, in ES7 version, there is a new method for Objects, the Object.values. Then it gets even simpler:

 const novaArray = array.map(Object.values);

Example:

const array = [{
  nomePergunta: 'resposta atualizada',
  quantidadeResposta: 5
}, {
  nomePergunta: 'esse foi',
  quantidadeResposta: 4
}];

const novaArray = array.map(Object.values);

console.log(JSON.stringify(novaArray));

  • Why const Sergio?

  • @Miguel I stopped using var :P

  • Ha, for some particular reason?

  • @Miguel const it’s safer, it’s the habit now... but it could be var, or let.

  • You didn’t have to be so radical, Sergio. Poor var... :D

  • @bfavaretto haha :)

  • Thanks @Sergio, it worked. Is it possible to popular with inverted values, in this case, first string after quantity? ex: [["updated reply, 5"],["this was ", 4]]

  • 1

    @Scooby yes it is, you can use the .reverse() https://jsfiddle.net/z3g5auod/

  • @Sergio Valeu, thank you so much for your help!

Show 4 more comments

6

With the Ecmascript 2017+ it is possible to do the following:

var arrays = objetos.map(function(obj){return Object.values(obj)});

Whereas objetos is your object array.

Chrome version 55, for example, already works.


As a suggestion from @Sergio , it is possible to simplify even more:

var arrays = objetos.map(Object.values);

2

Another approach is to use a classic iterator (although I prefer map), read the properties of the object, add in a temporary array to add in the new array of arrays:

var array = [{
    nomePergunta: 'resposta atualizada',
    quantidadeResposta: 5
}, {
    nomePergunta: 'esse foi',
    quantidadeResposta: 4
}];

var novoArray = [];

array.forEach(function(item){
  var tmp = [];
  for (var property in item){
    tmp.push(item[property]);
  }
  novoArray.push(tmp);
});

console.log(JSON.stringify(novoArray));

Browser other questions tagged

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