首页 > 解决方案 > 从 Javascript 中的 groupedBy 数组中删除和计算重复项

问题描述

我有一个数组,应该以删除和计算每个房间的重复灯的方式对其进行修改。目前我有以下数组:

在此处输入图像描述

我想让它们按room. 我lodash为此使用该功能:

  groupByAndCount(lamps) {
      const groupedArray = groupBy(lamps, function(n) {
        return n.pivot.room;
      });

      return groupedArray;
    },

这将返回这样的数组: 在此处输入图像描述

基本上,每个分组Object看起来都与第一个数组中的对象相同。但现在我想使用我编写的函数来计算和删除重复项:

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;
},

结果应该是一个像第一个一样的数组。但应该包含这个:

所以对象应该是这样的:

id
name:
fitting:
light_color_code:
dimmability:
shape:
price:
watt:
lumen:
type:
article_number:
room:
count:

但我无法让这些函数一起工作,所以它返回了这个数组。任何人都可以帮助我朝着正确的方向前进吗?在此先感谢,任何帮助表示赞赏。感觉就像我快到了。

一些样本数据:

{
  "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"
  }
}

标签: javascriptarraysjavascript-objects

解决方案


由于在 group by 中,您正在创建一个将键和数组作为其值的对象,因此您必须首先迭代对象(您可以使用Object.entries()),然后是数组。

尝试将分组的对象传递给下面的函数,你应该得到正确计数的数组。这是一个可以使用的示例 - https://stackblitz.com/edit/js-d2xcn3

注意:我已经使用pivot.lamp_id(并将其添加到对象中)用作地图的键。您可以选择将其更改为其他一些独特的属性。

  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;
}

推荐阅读