Home > OS >  Create an array of objects from another array of Objects with same keys
Create an array of objects from another array of Objects with same keys

Time:04-29

I want to create a new array of array which contain objects loop through an array.

const data = [{pizza: {quantities: 5, price: 12}, burger: {quantities: 3, price: 10}, fries: {quantities: 3, price: 6}}, {pizza: {quantities: 3, price: 20}, burger:{quantities: 4, price: 18}, fries: {quantities: 6, price: 8}}]

convert the above array to

[[{quantities: 5, price: 12},{quantities: 3, price: 20}],[{quantities: 3, price: 10},quantities: 4, price: 18}], [{quantities: 3, price: 6},{quantities: 6, price: 8}]]

which is make same key objects together. I am new for coding and I really appreciate any helps. Thank you!

CodePudding user response:

May be this helps, I will also add in few moments if some other ways are possible but this works :

var obj = [{pizza: {quantities: 5, price: 12}, burger: {quantities: 3, price: 10}, fries: {quantities: 3, price: 6}},  {pizza: {quantities: 3, price: 20}, burger:{quantities: 4, price: 18}, fries: {quantities: 6, price: 8}}]
var pizza=[];
var burger=[];
var fries=[];
var ans=[]
for(var i=0;i<obj.length;i  ){
  const oPizza = obj[i].pizza;
  const oBurger = obj[i].burger;
  const oFries = obj[i].fries;
  pizza.push(oPizza)
  burger.push(oBurger)
    fries.push(oFries)

}
ans.push(pizza);
ans.push(burger);
ans.push(fries);


console.log(ans)

Will add other better approach in this if data comes in same format then simply distribute it according to the index and we will not be required to write every variables of our own:

var obj = [{pizza: {quantities: 5, price: 12}, burger: {quantities: 3, price: 10}, fries: {quantities: 3, price: 6}},  {pizza: {quantities: 3, price: 20}, burger:{quantities: 4, price: 18}, fries: {quantities: 6, price: 8}}]

var ans=[]
obj.map((v)=>{
  var o = v;
  Object.keys(o).map((k,i)=>{
    
    if(ans[i]===undefined)
      ans[i]=[]
      
    ans[i].push(o[k])
  })
})
  console.log(ans)

CodePudding user response:

I'm assuming you want to get each kind of object (pizza, burger, etc) into its own array. You'll need to create a buffer object for that, then loop through the data array with forEach, create an object in the buffer for each key, and push each object into an array contained in that key:

buffer = {};

data.forEach(d=>{
  for(o in d) {
    buffer[o] = buffer[o] || [];
    buffer[o].push(d[o]);
  }
});

A couple of things. In the forEach, the d inside the parentheses is each element of the array as it loops. I just chose d arbitrarily. You could call this variable anything. I'm using arrow functions, so => connects the passed object (d) to the function operations (contained in {}, just like any function would be).

Inside the loop, I use for..in to iterate the object that is passed by forEach, and since it's not an array that's probably the easiest way. o in this case will be the key of the object, but you could use any name. This line:

buffer[o] = buffer[o] || [];

exists to check and see if the key o is already present in the buffer object, and if it isn't to assign an empty array to it. || is the OR operator. [] is just an empty array. If buffer[o] exists, then the || operator doesn't have to look any further, and just returns the existing value. If it doesn't exists, the || returns the value to the right of the operator, in this case the empty array [].

This line:

buffer[o].push(d[o]);

just pushes the value of the current object (d[o]) to the buffer object.

At this point, you have all of the objects from the original array sorted by key into the buffer object. If you really want to just return the array of values, then you use

Object.values(buffer)

to return just those, and that should give the format you specified. This technique doesn't care what the key names are, so you don't have to know in advance and you don't have to set up different arrays for each possible value.

Hope this helps and the explanation was clear. Let me know if this worked for you.

const data = [{pizza: {quantities: 5, price: 12}, burger: {quantities: 3, price: 10}, fries: {quantities: 3, price: 6}},  {pizza: {quantities: 3, price: 20}, burger:{quantities: 4, price: 18}, fries: {quantities: 6, price: 8}}];

buffer = {};

data.forEach(d=>{
  for(o in d) {
    buffer[o] = buffer[o] || [];
    buffer[o].push(d[o]);
  }
});

console.log("THE BUFFER:");
console.log(buffer);

console.log("THE ANSWER:");
console.log(Object.values(buffer));

  • Related