I have an array which should be modified in a way that it removes and counts duplicate lamps for each room. Currently I have the following array:
I want to have them grouped by room. I use the lodash function for that:
groupByAndCount(lamps) {
const groupedArray = groupBy(lamps, function(n) {
return n.pivot.room;
});
return groupedArray;
},
This returns the array like this:

Basically each grouped Object looks the same like the objects in the first array. But now I want to use the function I wrote to count and remove the duplicates:
removeAndCountDuplicates(lamps) {
// map to keep track of element
// key : the properties of lamp (e.g name, fitting)
// value : obj
let map = new Map();
// loop through each object in Order.
lamps.forEach(data => {
// loop through each properties in data.
let currKey = JSON.stringify(data.name);
let currValue = map.get(currKey);
// if key exists, increment counter.
if (currValue) {
currValue.count += 1;
map.set(currKey, currValue);
} else {
// otherwise, set new key with in new object.
let newObj = {
id: data.id,
name: data.name,
fitting: data.fitting,
light_color_code: data.light_color_code,
dimmability: data.dimmability,
shape: data.shape,
price: data.price,
watt: data.watt,
lumen: data.lumen,
type: data.type,
article_number: data.article_number,
count: 1,
image: data.image
// room: data.pivot.room
};
map.set(currKey, newObj);
}
});
// Make an array from map.
const res = Array.from(map).map(e => e[1]);
return res;
},
The result should be an array like the first one. But should contain this:
- 1 Lamp for each room with the count of how many times it occurs
So the object should be like this:
id
name:
fitting:
light_color_code:
dimmability:
shape:
price:
watt:
lumen:
type:
article_number:
room:
count:
But I couldn't manage to get the functions work together so it returns this array. Can anyone help me in the right direction? Thanks in advance, any help is appreciated. It feels like i'm almost there.
Some sample data:
{
"id": 3,
"name": "Noxion Lucent LED Spot PAR16 GU10 4W 827 36D | Extra Warm Wit - Vervangt 50W",
"fitting": "GU10",
"light_color_code": "2700K - 827 - Zeer warm wit",
"dimmability": 0,
"shape": "Spot",
"price": 2.44,
"watt": 4,
"lumen": 370,
"type": "LED ",
"article_number": 234987,
"pivot": {
"order_id": 2,
"lamp_id": 3,
"room": "Garage"
},
"image": {
"id": 3,
"lamp_id": 3,
"name": "234987",
"path": "/storage/234987.jpg"
}
}
