javascript - 从 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;
},
结果应该是一个像第一个一样的数组。但应该包含这个:
- 每个房间 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"
}
}
解决方案
由于在 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;
}
推荐阅读
- c++ - 在 C++ 中使用“fopen”打开的 mmap 文件
- netlogo - Netlogo:如何要求链接邻居做某事
- linux - 如何在linux中读取最后n分钟的日志文件
- python - 如何在导入行时直接使用“keras”而不是使用“tensorflow.keras”
- twig - 从 symfony 4 中的树枝模板调用服务
- apache - Camel FTPS连接登录失败
- r - 当我尝试在 R 中使用 sqldf 函数执行以下 DML 查询时,它会向我抛出一条错误消息
- c++ - For 循环没有运行 C++
- flutter - Flutter:如何翻转图标以获得镜像效果?
- database - Laravel比较不同表中的三列返回0项