Manipulate Object to group based on Array Object List in JavaScript


The Given problem is stating to manipulate objects to group based on the output array object list with the help of javascript.

Understanding the problem

Before starting to write algorithms and programmes for the given problem first we will understand the logic behind the problem.

Consider a set of objects that each represents a person and has details about that individual, such as person, age, and profession. We need to make a group of persons with the same profession. For instance, we wish to build an object whose properties each indicate a job and whose values are an array of objects with that job.

We may simply conduct operations on subsets of items that share a common characteristic by grouping objects based on a specific feature. This is a typical JavaScript operation, particularly when working with sets of data that comprise objects with various properties.

Algorithm − Using forEach()

Step 1: Construct a blank object with the name "grouped ".

Step 2: Iterate over the objects in the array.

Step 3: For each object, determine whether the term "grouped" contains a property matching the object's group name.

Step 4: If the property is present, Then add it to the array of objects for that particular group.

Step 5: If the property is not there, then create an array containing the current object and set its value as the property's value for the "grouped " property with the group name.

Step 6: In the last step show the output as grouped.

Example

//Define array of objects
const persons = [
  { person: 'Alka', profession: 'Choreographer', age: 25 },
  { person: 'Bittu', profession: 'Developer', age: 22 },
  { person: 'Chetana', profession: 'Teacher', age: 30 },
  { person: 'Rina', profession: 'Cricketer', age: 35 },
  { person: 'Sanchi', profession: 'Engineer', age: 23 },
  { person: 'Farhad', profession: 'Businessman', age: 28 },
];
//method groupAndObjects with arguments 
function groupAndObjects(array, prop) {
const grouped = {};
  array.forEach((obj) => {
    const gName = obj[prop];
    if (grouped[gName]) {
      grouped[gName].push(obj);
    } else {
      grouped[gName] = [obj];
    }
  });
    return grouped;
}
const group = groupAndObjects(persons, "person");
console.log(group);

Output

{
  Alka: [ { person: 'Alka', profession: 'Choreographer', age: 25 } ],
  Bittu: [ { person: 'Bittu', profession: 'Developer', age: 22 } ],
  Chetana: [ { person: 'Chetana', profession: 'Teacher', age: 30 } ],
  Rina: [ { person: 'Rina', profession: 'Cricketer', age: 35 } ],
  Sanchi: [ { person: 'Sanchi', profession: 'Engineer', age: 23 } ],
  Farhad: [ { person: 'Farhad', profession: 'Businessman', age: 28 } ]
}

Algorithm − Using reduce()

Step 1: Construct a sample array of objects with the name "items".

Step 2: Create a groupedItems to use reduce method and accumulate every object of array.

Step 3: Check if−else conditions to verify that the coming pair is matching that category or not.

Step 4: If it is matching with the category in the array then push it.

Step 6: In the last step show the output as groupedItems.

Example

// sample array of objects
const items = [
  { name: 'apple', category: 'fruit' },
  { name: 'banana', category: 'fruit' },
  { name: 'carrot', category: 'vegetable' },
  { name: 'orange', category: 'fruit' },
  { name: 'spinach', category: 'vegetable' }
];
// group items by category using reduce
const groupedItems = items.reduce((acc, item) => { 
  if (!acc[item.category]) { 
    acc[item.category] = [];
  }
  acc[item.category].push(item);
  return acc;
}, {});
// print the grouped items
console.log(groupedItems);

Output

{
  fruit: [
    { name: 'apple', category: 'fruit' },
    { name: 'banana', category: 'fruit' },
    { name: 'orange', category: 'fruit' }
  ],
  vegetable: [
    { name: 'carrot', category: 'vegetable' },
    { name: 'spinach', category: 'vegetable' }
  ]
}

Complexity

As per the above algorithm we have a time complexity of O(n). In this time complexity, n represents objects in the array. The reason for this complexity, the program iterates for each object present in the array only once. Just like time complexity, the space complexity of this algorithm is the same, O(n), as we know the grouped object will contain n properties, each with an array of objects with a total number of n.

Conclusion

As we have seen the logic for the given problem. To solve any problem we need to follow certain steps and think logically. Time complexity and space complexity are two measures of the efficiency of an algorithm. The above algorithms have the same time and space complexities.

Updated on: 23-Aug-2023

155 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements