数组过滤出多重复项时生成可展示错误详情数组的优化方案咨询
数组过滤出多重复项时生成可展示错误详情数组的优化方案咨询
我有一个从Excel表格提取的对象数组,目前使用.filter()方法对比主对象的特定键来返回新数组,处理逻辑如下:
- 如果返回数组长度为0,就不把额外数据合并到原对象中
- 如果返回数组长度为1,就将其键展开到当前对象,之后按需替换内容
- 但如果数组长度大于1,我想要给用户展示找到的重复项详情,让他们可以修正表格后重试
现有代码示例
/* Current user I am getting from for looping the firstSheet */ let currentUser = {'First Name': 'Billy', 'Last Name': 'Bob', 'Member Number': 3333,'Home Phone': 9764582917, 'Cell Phone': 2345677123, 'Day Phone': 9928844574, 'Email': 'seeBill@gmail.com', 'plan price': '10.10' } /* this is found from checking further in the first sheet by a small for loop that runs as it finds duplicates, ussaly there is only 1 object in this. There is a line per plan*/ let dupeArray = [{'First Name': 'Billy', 'Last Name': 'Bob', 'Member Number': 1111,'Home Phone': 9764582917, 'Cell Phone': 2345677123, 'Day Phone': 9928844574, 'Email': 'seeBill@gmail.com', 'plan Price': '10.10' }] let secondSheet = [ {'First Name': 'Billy', 'Last Name': 'Bob', 'plan1': 1111, 'plan2': 2222, 'plan3': 3333, 'Home Phone': 9764582917, 'Cell Phone': 2345677123, 'Day Phone': 9928844574, 'Email': 'seeBill@gmail.com', 'plan1 Price': '10.10', 'plan2 Price': '10.10', 'plan3 Price': '10.10' }, {'First Name': 'Benny', 'Last Name': 'Socks', 'plan1': 5315, 'plan2': 5562, 'plan3': 3333, 'Home Phone': 1122331467, 'Cell Phone': 9867485926, 'Day Phone': 1759603752, 'Email': 'seeBennySocks@gmail.com', 'plan1 Price': '10.10', 'plan2 Price': '10.10', 'plan3 Price': '10.10' }, {'First Name': 'Sara', 'Last Name': 'Jones', 'plan1': 5645, 'plan2': 5522, 'plan3': 6678, 'Home Phone': 6672854673, 'Cell Phone': 7766552897, 'Day Phone': 9856301788, 'Email': 'seeSara@gmail.com', 'plan1 Price': '10.10', 'plan2 Price': '10.10', 'plan3 Price': '10.10' }, {'First Name': 'Jones', 'Last Name': 'Smith', 'plan1': 2344, 'plan2': 7766, 'plan3': 8763, 'Home Phone': 2221113323, 'Cell Phone': 7766552897, 'Day Phone': 1115566779, 'Email': 'seeJones@gmail.com', 'plan1 Price': '10.10', 'plan2 Price': '10.10', 'plan3 Price': '10.10' }, ] let existingContacts; function handleUpdate(){ existingContacts = secondSheet.filter((currContact) => { if( [currContact['plan1'], currContact['plan2'], currContact['plan3']].indexOf(currentUser['memberNumber']) > -1 || currContact['Email'] === currentUser['Email'] || currContact['Cell Phone'] === currentUser['Cell Phone'] ) { return true; } else { for (let a = 0; a < dupeArray.length; a++) { if([currContact['plan1'], currContact['plan2'], currContact['plan3'],].indexOf(dupeArray[a]['Member Number']) > -1) { return true } } return false; } }) if (existingContacts.length === 1) {return true;} else if (existingContacts.length > 1) { /* Wanna push was found in the .filter() here */ return false; } else { return false; } } console.log(handleUpdate()) /* This is just to explain why the function itself returns a boolean */ // filteredSheet.push({ // ...(handleUpdate() && existingContacts[0]), // /* then I replace what i need to from currentUser here */ // })
我已经处理了一些细节,比如转换值避免空邮箱之类的错误,现在只需要实现:当找到多个重复项时,生成一个可以传给弹窗组件的详情数组,示例错误日志如下:
Error Log:
Billy Bob: duplicate with Benny Socks at Member Number
...etc
我的初步思路
我想到的方法是在函数开头声明modalArray,然后在existingContacts.length > 1的分支里遍历数组,逐一判断重复原因并添加到数组中,但感觉这是 brute force(暴力解法),不够优雅:
else if (existingContacts.length > 1) { let modalArray = []; // 需要在函数顶部声明 for(let b=0; b < existingContacts.length; b++) { if([existingContacts[b]['plan1'], existingContacts[b]['plan2'], existingContacts[b]['plan3']].indexOf(currentUser['memberNumber']) > -1) { modalArray.push({ currentUserFirstName: currentUser['First Name'], currentUserlastName: currentUser['Last Name'], issueFound: 'Member Number', currContactFirstName: existingContacts[b]['First Name'], currContactLastName: existingContacts[b]['Last Name']}) } else if (existingContacts[b]['Email'] === currentUser['Email']) { modalArray.push({ currentUserFirstName: currentUser['First Name'], currentUserlastName: currentUser['Last Name'], issueFound: 'Email', currContactFirstName: existingContacts[b]['First Name'], currContactLastName: existingContacts[b]['Last Name']}) } else if (existingContacts[b]['Cell Phone'] === currentUser['Cell Phone']) { modalArray.push({ currentUserFirstName: currentUser['First Name'], currentUserlastName: currentUser['Last Name'], issueFound: 'Cell Phone', currContactFirstName: existingContacts[b]['First Name'], currContactLastName: existingContacts[b]['Last Name']}) } else { for (let c = 0; c < dupeArray.length; c++) { // lol if([existingContacts[b]['plan1'], existingContacts[b]['plan2'], existingContacts[b]['plan3'],].indexOf(dupeArray[c]['Member Number']) > -1) { modalArray.push({ currentUserFirstName: currentUser['First Name'], currentUserlastName: currentUser['Last Name'], issueFound: 'Member Number', currContactFirstName: existingContacts[b]['First Name'], currContactLastName: existingContacts[b]['Last Name']}) } } } } return false; }
我希望能找到更简洁、可维护的方法来生成这个可以传给弹窗组件的数组。
备注:内容来源于stack exchange,提问作者Zu0s
相关产品推荐
相关产品推荐

