1. let portalAdminister = [
    2. {
    3. children: [
    4. {
    5. children: [
    6. {
    7. label: "小明",
    8. value: "01380352611209037"
    9. },
    10. {
    11. label: "小红",
    12. value: "025609424537722615"
    13. }
    14. ],
    15. disabled: true,
    16. label: "班主任老张",
    17. selectable: false,
    18. value: "515794864"
    19. },
    20. {
    21. label: "小白",
    22. value: "0116415710-843089919"
    23. },
    24. {
    25. label: "小绿",
    26. value: "01195630266932644008"
    27. },
    28. {
    29. label: "小黑",
    30. value: "025609424537722615"
    31. }
    32. ],
    33. disabled: true,
    34. label: "一年级二班",
    35. selectable: false,
    36. value: "1"
    37. }
    38. ]
    39. let nameArr = []
    40. let keyArr = []
    41. let findVal = "025609424537722615"
    42. function flatLoop(item) {
    43. return item.map(x => {
    44. if (x instanceof Array) {
    45. flatLoop(x)
    46. } else if (x instanceof Object) {
    47. if (x.value) {
    48. nameArr.push(x.value)
    49. keyArr.push(x.label)
    50. }
    51. flatLoop(Object.values(x))
    52. }
    53. })
    54. }
    55. flatLoop(portalAdminister)
    56. // 给字段赋值属性
    57. let newAdminsters = keyArr.map((label, i) => (
    58. {
    59. label,
    60. value: nameArr[i]
    61. }
    62. ))
    63. console.log('处理后的数组对象', newAdminsters)
    64. // 将需要查找的值转换为字符串数组
    65. let find = findVal.split(",")
    66. const filterByKey = (find = [], newAdminsters = []) => {
    67. let res = [];
    68. // 筛选
    69. res = newAdminsters.filter(el => {
    70. const { value } = el;
    71. const index = find.indexOf(value);
    72. return index !== -1;
    73. });
    74. return res;
    75. };
    76. // 返回筛选后的数组对象
    77. let arrAdminster = filterByKey(find, newAdminsters);
    78. console.log('arrAdminster', arrAdminster)
    79. for (let i = 0; i < arrAdminster.length; i++) {
    80. for (let j = i + 1; j < arrAdminster.length; j++) {
    81. if (arrAdminster[i].value == arrAdminster[j].value) {
    82. arrAdminster.splice(j, 1);
    83. j--;
    84. }
    85. }
    86. }
    87. console.log("去重后的数组:", arrAdminster)
    88. let portalAdministerId = arrAdminster.map(item => item.value).join(",")
    89. let portalAdministerName = arrAdminster.map(item => item.label).join(",")
    90. console.log("筛选之后的结果:", portalAdministerId, portalAdministerName)