0

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:

enter image description here

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: enter image description here

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"
  }
}
2
  • Can you maybe add some sample data to it? Commented Mar 31, 2020 at 11:48
  • I added an example of how the objects in the first array look like. @Vandesh in the end I want for each lamp show the count of duplicates for each room. Commented Mar 31, 2020 at 11:57

1 Answer 1

1

Since in the group by, you are creating an object with keys and arrays as it's values, you have to first iterate on the object (you can use Object.entries()) and then it's arrays.

Try passing the grouped object to the function below and you should get the array with correct counts. Here's a sample to play with - https://stackblitz.com/edit/js-d2xcn3

NOTE: I have used pivot.lamp_id (and added it in the object as well) to use as the key for the map. You can choose to change it to some other unique property.

  const removeDuplicates = (lamps) => {
  let map = new Map();
  Object.entries(lamps).forEach(([key, value])=> {
    // loop through each object in Order.
  lamps[key].forEach(data => {
    // loop through each properties in data.
    let currKey = JSON.stringify(data.pivot.lamp_id);
    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 = {
        lamp_id: data.pivot.lamp_id,
        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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

I will take a look at it in a moment, not near my computer now. I will respond ASAP
I get the following error: lamps[key].forEach is not a function. I think this is because when I console.log(lamps[key]) it returns an object instead of an array.
If you check how I have passed it in the StackBlitz link, you can compare if you're missing something
Your answer does work in the StackBlitz link. I am going to check it soon, thanks for your answers. I'll let you know if it worked in my application!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.