Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中从groupedBy数组中删除并计数重复项_Javascript_Arrays_Javascript Objects - Fatal编程技术网

在Javascript中从groupedBy数组中删除并计数重复项

在Javascript中从groupedBy数组中删除并计数重复项,javascript,arrays,javascript-objects,Javascript,Arrays,Javascript Objects,我有一个阵列,应该进行修改,以消除和计数每个房间的重复灯。目前我有以下阵列: 我想把它们按房间分组。我使用lodash函数进行以下操作: groupByAndCount(lamps) { const groupedArray = groupBy(lamps, function(n) { return n.pivot.room; }); return groupedArray; }, 这将返回如下所示的数组: 基本上,每个分组的

我有一个阵列,应该进行修改,以消除和计数每个房间的重复灯。目前我有以下阵列:

我想把它们按房间分组。我使用lodash函数进行以下操作:

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

      return groupedArray;
    },
这将返回如下所示的数组:

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

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;
},
结果应该是与第一个数组类似的数组。但应包含以下内容:

每个房间1盏灯,并记录其出现的次数 所以对象应该是这样的:

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

由于在GROUPBY中,您创建的对象的值是键和数组,因此必须首先迭代可以使用的对象,然后迭代它的数组

尝试将分组对象传递给下面的函数,您应该获得具有正确计数的数组。 这是一个可以玩的示例-

注意:我使用了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;
}

你能给它添加一些示例数据吗?我添加了一个示例,说明第一个数组中的对象是什么样子的@Vandesh最后,我想让每盏灯显示每个房间的复灯数量。我会马上看一看,现在不要靠近我的电脑。我将在收到以下错误后尽快响应:lamps[key]。forEach不是函数。我想这是因为当我控制台.loglamps[key]时,它返回的是一个对象而不是数组。如果你检查我在StackBlitz链接中是如何传递它的,你可以比较你是否遗漏了一些东西。你的答案在StackBlitz链接中确实有效。我很快会查的,谢谢你的回答。我会让你知道,如果它在我的申请工作!