为什么对象中的所有值都在 lodash 映射函数中更新?

Why all the values in the object are being updated inside lodash map function?

我在这里尝试根据键更新对象,但是在执行映射函数后,无论我是否尝试过滤键,所有值都会更新。

const statusList = {
  New: {
    id: [
      0,
    ],
    name: "New",
    count: 0,
  },
  In_Progress: {
    id: [
      1,
      2,
      3,
    ],
    name: "In_Progress",
    count: 0,
  },
  All_Active: {
    id: [
      0,
      1,
      2,
      3,
    ],
    name: "All_Active",
    count: 0,
  },
  On_Hold: {
    id: [
      19,
    ],
    name: "On_Hold",
    count: 0,
  },
  Selected: {
    id: [
      4,
    ],
    name: "Selected",
    count: 0,
  },
  Rejected: {
    id: [
      5,
    ],
    name: "Rejected",
    count: 0,
  },
  Withdraw: {
    id: [
      6,
    ],
    name: "Withdraw",
    count: 0,
  },
  Hired: {
    id: [
      100,
    ],
    name: "Hired",
    count: 0,
  },
};

const jobCandidateStatusData = {
  "364": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 0,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 0,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 0,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 0,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "440": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 0,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 0,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 0,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 0,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "7335": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 0,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 0,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 0,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 0,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "7336": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 0,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 0,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 0,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 0,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "7337": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 0,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 0,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 0,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 0,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "7338": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 0,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 0,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 0,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 0,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "7339": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 0,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 0,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 0,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 0,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "7340": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 0,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 0,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 0,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 0,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "7367": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 0,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 0,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 0,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 0,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "7415": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 0,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 0,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 0,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 0,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
};

const candidateStatusList =  {
  "0": {
    CANDIDATE_ID: 1799,
    JOB_ID: 7415,
    MAPPING_STATUS: 3,
  },
  "1": {
    CANDIDATE_ID: 1801,
    JOB_ID: 7415,
    MAPPING_STATUS: 2,
  },
  "2": {
    CANDIDATE_ID: 1802,
    JOB_ID: 7415,
    MAPPING_STATUS: 1,
  },
  "3": {
    CANDIDATE_ID: 1806,
    JOB_ID: 7415,
    MAPPING_STATUS: 1,
  },
  "4": {
    CANDIDATE_ID: 1696,
    JOB_ID: 440,
    MAPPING_STATUS: 4,
  },
  "5": {
    CANDIDATE_ID: 1697,
    JOB_ID: 440,
    MAPPING_STATUS: 4,
  },
  "6": {
    CANDIDATE_ID: 1694,
    JOB_ID: 440,
    MAPPING_STATUS: 1,
  },
  "7": {
    CANDIDATE_ID: 1592,
    JOB_ID: 364,
    MAPPING_STATUS: 1,
  },
  "8": {
    CANDIDATE_ID: 1595,
    JOB_ID: 364,
    MAPPING_STATUS: 4,
  },
  "9": {
    CANDIDATE_ID: 1599,
    JOB_ID: 364,
    MAPPING_STATUS: 0,
  },
}

_.map(candidateStatusList, ({ JOB_ID, MAPPING_STATUS }) => {
      const status = {...jobCandidateStatusData[JOB_ID]};
      switch(MAPPING_STATUS){
        case 0: 
          status.New.count+=1;
          status.All_Active.count+=1;
          break;
        case 1:
        case 2:
        case 3:
          status.In_Progress.count+=1;
          status.All_Active.count+=1;
          break;
        case 4: 
          status.Selected.count+=1;
          break;
        case 5:
          status.Rejected.count+=1;
          break;
        case 6:
          status.Withdraw.count+=1;
          break;
        case 19:
          status.On_Hold.count += 1;
          break;
        default:
          break;
      }
      jobCandidateStatusData[JOB_ID] = {...status};
      return 0;
    })

这是执行 map 函数后我得到的最终响应...我不明白对象中的每个值是如何更新的。

const jobCandidateStatusData = {
  "364": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 1,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 6,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 7,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 3,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "440": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 1,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 6,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 7,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 3,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "7335": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 1,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 6,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 7,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 3,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "7336": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 1,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 6,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 7,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 3,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "7337": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 1,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 6,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 7,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 3,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "7338": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 1,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 6,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 7,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 3,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "7339": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 1,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 6,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 7,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 3,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "7340": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 1,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 6,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 7,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 3,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "7367": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 1,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 6,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 7,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 3,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
  "7415": {
    New: {
      id: [
        0,
      ],
      name: "New",
      count: 1,
    },
    In_Progress: {
      id: [
        1,
        2,
        3,
      ],
      name: "In_Progress",
      count: 6,
    },
    All_Active: {
      id: [
        0,
        1,
        2,
        3,
      ],
      name: "All_Active",
      count: 7,
    },
    On_Hold: {
      id: [
        19,
      ],
      name: "On_Hold",
      count: 0,
    },
    Selected: {
      id: [
        4,
      ],
      name: "Selected",
      count: 3,
    },
    Rejected: {
      id: [
        5,
      ],
      name: "Rejected",
      count: 0,
    },
    Withdraw: {
      id: [
        6,
      ],
      name: "Withdraw",
      count: 0,
    },
    Hired: {
      id: [
        100,
      ],
      name: "Hired",
      count: 0,
    },
  },
}

好吧,代码看起来很混乱,但最终的结论是 当我们尝试传播或克隆多级对象时,永远不会创建克隆,但新变量指向现有对象。

因此我们需要通过编写自定义函数或使用 lodash cloneDeep 函数来深度克隆对象。