前衬套工艺代码重复检测
以下是使用ES6方法实现判断数组中是否有相同的materialCode的数据,并判断相同materialCode中的craftCode是否存在相同的值,并将相同的craftCode所在的对象的索引打印出来的代码:
const data = [
{
"del": 0,
"createBy": "GW00303172",
"createTime": "2023-10-16 11:17:16",
"updateBy": "GW00303172",
"updateTime": "2023-10-16 11:17:16",
"id": 2685,
"processId": 1383,
"projectId": 35,
"productCode": "PA 20230905 739d896a58",
"productBomCode": "20230905 11 63d3244aa7",
"materialCode": "PA 20230905 739d896a58",
"materialName": "前衬套",
"electrophoreticArea": 200,
"weldBeadLength": 100,
"roughWeight": 2,
"materialCategory": "2",
"materialType": 0,
"supplierStatus": "1",
"materialStatus": "3",
"importanceLevel": "1",
"sourceFrom": "0",
"yieldCode": null,
"level": 1,
"weight": 1.02,
"specifications": "--",
"borrowRelation": "J65",
"num": 1,
"targetPrice": 66,
"other": "1",
"projectCode": "1",
"materialId": 2685,
"deliveryAddress": "5",
"passRatio": "5",
"craftCode": "GY000105",
"craftSubType": "3",
"type1": "0",
"type2": "0",
"type3": "0",
"productionPlaceCode": "CD0003"
},
// ...其他数据
];
// 使用Map来记录materialCode和对应的索引数组
const materialCodeMap = new Map();
// 遍历数组
data.forEach((obj, index) => {
// 获取当前对象的materialCode和craftCode
const { materialCode, craftCode } = obj;
// 判断是否已经存在相同的materialCode
if (materialCodeMap.has(materialCode)) {
const indexArray = materialCodeMap.get(materialCode);
// 判断相同materialCode中是否有相同的craftCode
const hasSameCraftCode = indexArray.some(i => data[i].craftCode === craftCode);
// 如果存在相同的craftCode,则打印索引
if (hasSameCraftCode) {
console.log(index);
}
// 将当前索引添加到索引数组中
indexArray.push(index);
} else {
// 如果不存在相同的materialCode,则添加新的键值对到Map中
materialCodeMap.set(materialCode, [index]);
}
});
以上代码会遍历数组,并使用Map来记录相同materialCode的对象的索引数组。在遍历过程中,对于每个对象,会检查当前的materialCode是否已经在Map中存在。如果存在,就遍历索引数组,判断是否有相同的craftCode,并打印索引。如果不存在,就将当前索引添加到索引数组中,并将新的键值对添加到Map中。
请注意,以上代码只是一种实现方法,具体根据实际情况进行调整和优化。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/pi3l 著作权归作者所有。请勿转载和采集!