节点 v12.6.0 中 array.reverse() 的问题

Problem with array.reverse() in node v12.6.0

将节点更新到最新版本后,我的测试中断,说我的数组没有反转。我有一个包含 4 个数字的数组,并开玩笑说它希望我的数组中的数字反转,但是在 运行 之后 .reverse() 它没有按预期进行。这是节点的一种新问题吗?

  it(`should handle ${SORT_MEMBERS}`, () => {
    const members = [{ ssn: 7 }, { ssn: 6 }, { ssn: 5 }, { ssn: 4 }];

    const attr = 'ssn';
    const order = SORT_ORDER.ASC;

    let newState = reducer(state, {
      type: INIT_MEMBERS,
      payload: { members },
    });

    newState = reducer(newState, {
      type: SORT_MEMBERS,
      payload: { attr, order }
    });

    expect(newState).toEqual({
      ...state,
      members: members.reverse(),
      sort: { attr, order },
    });

  });

初始状态:

const initialState = {
  batchUpdates: {
    salaryChangePercentage: '',
    applyDate: null,
  },
  form: {
    ableToWork: null,
    memberErrors: null,
  },
  updates: {}, // hash with ssn as key
  members: [],
  errors: {},
  sort: {
    attr: null,
    order: null,
  },
  saving: false,
  saved: false,
  saveResult: null,
  saveError: null,
};

案例SORT_MEMBER:

 case SORT_MEMBERS:
      return {
        ...state,
        members: sortMembers(state.members, state.updates, action.payload.attr, action.payload.order),
        sort: {
          attr: action.payload.attr,
          order: action.payload.order,
        },
      };

最后是我的排序方法:

export const sortMembers = (members, updates, attr, order) => {
  const sorted = members.slice();
  const path = attr.split('.');
  const byUpdates = path.length === 2 && path[0] === 'updates';

  if (!byUpdates) {
    return sorted.sort((a, b) => (
      order === SORT_ORDER.DESC ? a[attr] < b[attr] : a[attr] > b[attr]
    ));
  }
  // Sort by updates on given ssn
  return sorted.sort((a, b) => (
    order === SORT_ORDER.DESC
      ? updates[a.ssn][path[1]] < updates[b.ssn][path[1]] : updates[a.ssn][path[1]] > updates[b.ssn][path[1]]
  ));
};

在我更新 Node.js

之前,它曾经有效

我找到了问题的解决方法!

以下代码中有一个错误类型:

order === SORT_ORDER.DESC ? a[attr] < b[attr] : a[attr] > b[attr]

应该有 - 和 +,而不是 > 和 <。正确的是:

order === SORT_ORDER.DESC ? a[attr] + b[attr] : a[attr] - b[attr]