gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 13:52:20 25 4
gpt4 key购买 nike

将节点更新到最新版本后,我的测试失败,说我的数组没有反转。我有一个包含 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]

关于javascript - 节点 v12.6.0 中 array.reverse() 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60264711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com