Combining objects with duplicate values inside an array

Nora

If I have an array of objects and there are certain objects that have the same name/property (in my case there are 2 instances of objects with a name of "highway"), how can I remove these objects from the array and add a new object with that contains data from both?

const data = [
    { "data": [ { "x": "sensor_error_code", "y": [ 0, 9 ] } ], "name": 123 },
    { "data": [ { "x": "road_type", "y": [ 15, 24 ] } ], "name": "city" },
    { "data": [ { "x": "road_type", "y": [ 0, 14 ] } ], "name": "highway" },
    { "data": [ { "x": "road_type", "y": [ 25, 30 ] } ], "name": "highway" },
    { "data": [ { "x": "weather", "y": [ 0, 8 ] } ], "name": "rain" },
    { "data": [ { "x": "weather", "y": [ 9, 24 ] } ], "name": "sun" },
    { "data": [ { "x": "special_object", "y": [ 5, 15 ] } ], "name": true }
];

e.g. having one instance of "highway" with a data object containing the values from both:

{ "data": [ { "x": "road_type", "y": [ 0, 14 ] }, { "x": "road_type", "y": [ 25, 30 ] } ], "name": "highway" }

So far I've tried to make a new empty object and loop over the data array, accumulating that empty object with names and values from that array. I was planning on converting that object to an array in the end. The code I've used is below:

const myData = {};
atts.forEach(att => {
  if (myData[att.name]) {
    myData[att.name].push(att.data)
  } else {
    myData[att.name] = att.data;
  }
})
console.log(myData)

However, the code I've used above involves some additional manipulation to make the data ready for consumption by a library and I was wondering if there are simpler ways to achieve my goal?

Kelvin Schoofs

Your solution has a decent start. There are only 2 problems with your solution:

  • You need to "unwrap" the array when pushing it
  • You need to combine them all together at the end
const myData = {};
atts.forEach(att => {
  if (myData[att.name]) {
    myData[att.name].push(...att.data) // Notice this change
  } else {
    myData[att.name] = att.data;
    // or if you don't want to alter the original data
    myData[att.name] = [...att.data]; // basically a shallow copy
  }
})
const result = [];
for (const key in myData) {
    result.push({ data: myData[key], name: key });
}
console.log(result);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to delete duplicate values of objects inside array?

combining duplicate values in JavaScript array

Combining objects in an array with similar values

removing duplicate objects inside of array

Grouping duplicate objects into an array of values

Combining objects in array based on key values

Combining objects in an array by common keys and values

combining rows for duplicate values

Combining values of json objects into an array with unique object keys and its values

How to duplicate values inside a numpy array?

How to remove duplicate values inside an array in MONGODB?

Remove duplicate values from an array of objects in javascript

How to remove both duplicate values in an array of objects

Laravel remove objects with duplicate values in array

How to merge duplicate values in array of objects

How to get duplicate values in an array of objects

Combining array of objects with common values and return a key and value for each

Show duplicate index for duplicate values in an array of objects in javascript

Pandas: combining duplicate index values

Flatten and combining an Array of Objects

filtring boolean values inside an array of objects

How to get all values of objects inside array

JSON values from objects inside an Array

Mongoose add/append values inside array inside array of objects

How to compare values in an array, that is inside an array of objects, with a property of the objects?

How to get an array of the values of all properties of objects inside an array of objects?

Array Combining Values

combining array values in looping

Return unique array values from an array inside an array of objects