Convert object to array of objects in JavaScript


In the given problem statement we are required to convert an object to an array of objects with the help of javascript functionalities. To perform conversion of an object into an array of objects we can use Javascript’s built−in methods.

What is an Array in Javascript ?

An array is a collection of similar data elements under one roof. So let's understand the overall working process of arrays in JavaScript. An array is an object that contains one or more elements. So it has some properties and methods that make working with arrays easier in JavaScript.

For example

const arr = [2, 4, 6, 8];

console.log(arr);

Output

[2, 4, 6, 8]

What is an Object in Javascript ?

In Javascript, an object is an entity with properties and its type. For example we have a car. Car is an object and it has some properties like color, model, design and weight and so on. Similarly an object has a property which is used to define its characteristic. Let’s see example of object with below code:

// Object example car object
const carObj = {
  name: "Audi",
  color: "red",
  wheels: 4,
  engine: { cylinders: 4, size: 2.2 }
};

// Function to display properties of car object
function display() {
  console.log("My car's name is " + carObj.name + " and it is " + carObj.color + " with " + carObj.wheels + " wheels.");
}

display()

Output

My car's name is Audi and it is red with 4 wheels.

What is an Array of Objects in Javascript ?

As we have seen above that array is a collection of similar types of data whereas an array of objects is an array which contains objects as its data. For example we have an array of objects with the name employees with some properties like name, designation and from.

const employees = [
  {
    "name": "Alok",
    "designation": "Software developer",
    "from": "Pune",
   
  },
  {
    "name": "Ashish",
    "designation": "Content developer",
    "from": "Mumbai",
   
  },
  {
    ...
  },
  …]

How to Convert an Array of Objects ?

In Javascript, we are required to perform conversion of objects into an array of objects with the help of array methods like map, filter and reduce methods. Or we can use a combination of these methods to create a new array of objects with the desired properties or values from the existing array. For example if we have a variety of elements that address items with name, cost and class.

Logic for The Above Problem

The easiest way to implement the above problem is using the predefined methods of javascript.

So let's understand the logic of the problem given. For conversion of objects into an array of objects we will use an array with the name objPerson which has some properties in it. Now after defining the array we need to iterate all the properties of the object and in doing this process we will use forEach method and then we will combine the repeated names and combine their properties to decrease the size of the array. After doing this we will push the new array of objects into arrayOfObject.

Example

const objPerson = {
  "Amit_Age": 32,
  "Amit_Height": 174,
  "Amit_Weight": 66,
  "Yogesh_Age": 29,
  "Yogesh_Height": 169,
  "Yogesh_Weight": 59
};
const arrayOfObject = (objPerson = {}) => {
  const res = [];
  Object.keys(objPerson).forEach(el => {
     const part = el.split('_');
     const person = part[0];
     const info = part[1].toLowerCase();
     if(!this[person]){
        this[person] = {
           "name": person
        };
        res.push(this[person]);
     }
     this[person][info] = objPerson[el];
  }, {});
  return res;
};
console.log(arrayOfObject(objPerson));

Output

[
  { name: 'Amit', age: 32, height: 174, weight: 66 },
  { name: 'Yogesh', age: 29, height: 169, weight: 59 }
]

Complexity

The following problem statement is solved using javascript methods like Object.keys and push method that traverses the array of elements in O(n) worst case time complexity . The space complexity of the problem statement is O(n) because it is storing n objects.

Conclusion

The following problem statement is solved using javascript methods like Object.keys and push method that traverses the array of elements in O(n) worst case time complexity . The space complexity of the problem statement is O(n) because it is storing n objects.

Updated on: 22-Aug-2023

403 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements