对于我的 React 应用程序,我构建了许多自定义中间件并在applyMiddleware(). 因为我有这么多,redux 存储文件看起来有点拥挤。将它们全部applyMiddleware()传递到函数中或将它们导入到函数内的单独文件中,然后将该函数传递到函数中applyMiddleware()以保持清洁是一种好习惯吗?
// Redux store
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
export const store = createStore(
reducers,
composeEnhancers(
applyMiddleware(...xMdl, ...yMdl, ...zMdl, ...nAmountsOfMdl),
)
);
Run Code Online (Sandbox Code Playgroud) In my react application, a user logs in and then directed to his/her dashboard upon authentication. In order to display all the data of the user in the dashboard I have a separate file in which there is a function called getUserData() which dispatches all the actions of the dashboard components and then getUserData() is called within loadUser() so every time the app renders it excutes loadUser(). Is this a good practice and an efficient way to display user data …
问题 -下面的算法循环遍历对象数组并将对象分配给三个子集,使得每个子集的总和非常接近(贪婪算法)。如果运行代码,您会注意到在第一个子集中 p:11 出现两次,在第三个子集中 p:10 出现两次。我不想让 p 值多次出现在同一个数组中。
问题 -如何在算法中确保 p 的值不会在同一子集数组中多次出现,因为对象被分配到子集数组,同时确保每个子集数组的总和仍然相同?
let list = [
{p:2, size:50},{p:4, size:50},{p:5,size:25},
{p:6, size:167},{p:6, size:167},{p:7, size:50},
{p:8, size:25},{p:8, size:50},{p:10, size:75},
{p:10, size:75},{p:11, size:25},{p:11, size:50},
{p:12, size:25},{p:13, size:50},{p:14,size:25}
]
function balance_load(power_load_array, number_of_phases) {
const sorted = power_load_array.sort((a, b) => b.size - a.size); // sort descending
const output = [...Array(number_of_phases)].map((x) => {
return {
sum: 0,
elements: [],
};
});
for (const item of sorted) {
const chosen_subset = output.sort((a, b) => a.sum …Run Code Online (Sandbox Code Playgroud)