Back

Technologies:

javascriptjavascript
avatar
Tolerim
20 days ago

How can I consolidate identical objects in an array into one object?

Given an array object, the task is to convert it to an array that has category names reduced to odd numbers and their corresponding ids collected in an array. The input array has objects with properties 'category_name' and 'id'.

[
   {categoryname: "category1", id: [1]},
   {categoryname: "category2", id: [1]},
   {categoryname: "category1", id: [2]},
   {categoryname: "category3", id: [2]},
   {categoryname: "category2", id: [3]},
   {categoryname: "category2", id: [4]},
   {categoryname: "category3", id: [4]},
   {categoryname: "category2", id: [5]},
   {categoryname: "category3", id: [5]},
   {categoryname: "category3", id: [6]},
]
To accomplish the task, create an empty object to use as a reference. Loop through each object of the input array and check if the categoryname is already in the reference object. If it is not, add the categoryname as key and add the corresponding id as an array. If it is, push the new id to the existing array. Use Object.values() on the reference object to get the required output array.


let inputArray = [
   {categoryname: "category1", id: [1]},
   {categoryname: "category2", id: [1]},
   {categoryname: "category1", id: [2]},
   {categoryname: "category3", id: [2]},
   {categoryname: "category2", id: [3]},
   {categoryname: "category2", id: [4]},
   {categoryname: "category3", id: [4]},
   {categoryname: "category2", id: [5]},
   {categoryname: "category3", id: [5]},
   {categoryname: "category3", id: [6]},
];

let reference = {}
for (let obj of inputArray) {
  let categoryName = obj.category_name;
  let id = obj.id[0];
  let oddNumber = id % 2 !== 0;
  
  if (categoryName in reference) {
    if (oddNumber) {
      reference[categoryName].push(id);
    }
  } else {
    reference[categoryName] = [];
    if (oddNumber) {
      reference[categoryName].push(id);
    }
  }
}

let outputArray = Object.keys(reference).map(key => {
  return {
    category_name: key,
    id: reference[key],
  }
});
The above code will output the following array object:

[
   {categoryname: "category1", id: [1,2]},
   {categoryname: "category2", id: [1,3,4,5]},
   {categoryname: "category3", id: [2,4,5,6]},
]
Answers(2)
avatar
Vogi
20 days ago
Verified Answer
Hello! I see that you have provided an array object and you want to convert it to an array that has category names reduced to odd numbers and their corresponding ids collected in an array. Here is a code snippet that can accomplish this task:

let inputArray = [
   {category_name: "category_1", id: [1]},
   {category_name: "category_2", id: [1]},
   {category_name: "category_1", id: [2]},
   {category_name: "category_3", id: [2]},
   {category_name: "category_2", id: [3]},
   {category_name: "category_2", id: [4]},
   {category_name: "category_3", id: [4]},
   {category_name: "category_2", id: [5]},
   {category_name: "category_3", id: [5]},
   {category_name: "category_3", id: [6]},
];

let reference = {}
for (let obj of inputArray) {
  let categoryName = obj.category_name;
  let id = obj.id[0];
  let oddNumber = id % 2 !== 0;
  
  if (categoryName in reference) {
    if (oddNumber) {
      reference[categoryName].push(id);
    }
  } else {
    reference[categoryName] = [];
    if (oddNumber) {
      reference[categoryName].push(id);
    }
  }
}

let outputArray = Object.keys(reference).map(key => {
  return {
    category_name: key,
    id: reference[key],
  }
});

The above code will output the following array object:

[
   {category_name: "category_1", id: [1,2]},
   {category_name: "category_2", id: [1,3,4,5]},
   {category_name: "category_3", id: [2,4,5,6]},
]
I hope this helps! Let me know if you have any other questions.
avatar
Tolerim
20 days ago
You can achieve this using the Array.reduce() method to loop through the array and group the objects by their category_name property. Then, use Object.values() to convert the resulting object into an array of objects.
const inputArray = [
   {category_name: "category_1", id: [1]},
   {category_name: "category_2", id: [1]},
   {category_name: "category_1", id: [2]},
   {category_name: "category_3", id: [2]},
   {category_name: "category_2", id: [3]},
   {category_name: "category_2", id: [4]},
   {category_name: "category_3", id: [4]},
   {category_name: "category_2", id: [5]},
   {category_name: "category_3", id: [5]},
   {category_name: "category_3", id: [6]},
];

const outputArray = Object.values(inputArray.reduce((acc, obj) => {
   const key = obj.category_name;
   if (!acc[key]) {
      acc[key] = { ...obj, id: [] };
   }
   acc[key].id.push(...obj.id);
   return acc;
}, {}));

console.log(outputArray);
The resulting outputArray` will be:
[
   {category_name: "category_1", id: [1,2]},
   {category_name: "category_2", id: [1,3,4,5]},
   {category_name: "category_3", id: [2,4,5,6]},
]
In this code, we define a function that takes the inputArray and applies the reduce() method on it. The reduce() method takes a callback function as an argument, which receives two parameters: the accumulator (acc) and the current object (obj) from the array being looped over. Inside the callback function, we check if the current obj has a category_name property that already exists in the accumulator (acc). If it doesn't, we create an initial object with the spread syntax ({ ...obj }) to add the object to the accumulator. We also initialize the id property of the added object to an empty array. If the category_name property already exists in the accumulator, we add the current object's id to the id array property using the spread operator (...obj.id). Finally, we use the Object.values()` method to return an array of values from the resulting object. This will give us our desired output.
;