我有一个要买的东西清单(对于我用 React 制作的一个小游戏),它是一个数组。它处于一种称为“市场”的状态。我还有另一个,我想随机化所有值并将它们存储在另一个状态。
这是我的市场清单:
let marketList = [
{"name": 'product name', "qty": 0, "minPrice": 10, "maxPrice": 100, "currentPrice": 0, "historyPrice": [], "historyQty": [], "available": false},
{"name": 'product name', "qty": 0, "minPrice": 30, "maxPrice": 200, "currentPrice": 0, "historyPrice": [], "historyQty": [], "available": false},
{"name": 'product name', "qty": 0, "minPrice": 100, "maxPrice": 1500, "currentPrice": 0, "historyPrice": [], "historyQty": [], "available": false},
{"name": 'product name', "qty": 0, "minPrice": 200, "maxPrice": 4000, "currentPrice": 0, "historyPrice": [], "historyQty": [], "available": false},
{"name": 'product name', "qty": 0, "minPrice": 50, "maxPrice": 6000, "currentPrice": 0, "historyPrice": [], "historyQty": [], "available": false},
{"name": 'product name', "qty": 0, "minPrice": 1, "maxPrice": 400, "currentPrice": 0, "historyPrice": [], "historyQty": [], "available": false},
{"name": 'product name', "qty": 0, "minPrice": 60, "maxPrice": 3450, "currentPrice": 0, "historyPrice": [], "historyQty": [], "available": false},
{"name": 'product name', "qty": 0, "minPrice": 2, "maxPrice": 120, "currentPrice": 0, "historyPrice": [], "historyQty": [], "available": false},
{"name": 'product name', "qty": 0, "minPrice": 8, "maxPrice": 600, "currentPrice": 0, "historyPrice": [], "historyQty": [], "available": false},
{"name": 'product name', "qty": 0, "minPrice": 120, "maxPrice": 3200, "currentPrice": 0, "historyPrice": [], "historyQty": [], "available": false},
{"name": 'product name', "qty": 0, "minPrice": 35, "maxPrice": 100, "currentPrice": 0, "historyPrice": [], "historyQty": [], "available": false},
{"name": 'product name', "qty": 0, "minPrice": 300, "maxPrice": 12000, "currentPrice": 0, "historyPrice": [], "historyQty": [], "available": false},
{"name": 'product name', "qty": 0, "minPrice": 1, "maxPrice": 80, "currentPrice": 0, "historyPrice": [], "historyQty": [], "available": false}
];
我想随机化其中的所有值并将随机列表存储在一个名为“明天”的状态中,以便能够提供有关下一个价格的提示。这是我随机排列列表的助手。
export function randomiseMarketList(list, day = 0){
for(let i = 0; i < list.length; i++) {
let item = list[i];
let historyQtylength = item.historyQty.length;
let random = randomAlgo(day);
item.currentPrice = Math.floor(Math.random() * (item.maxPrice - item.minPrice)) + item.minPrice;
item.qty = random;
item.available = !(day == 0 || item.qty < 1);
item.historyPrice.push(item.currentPrice);
item.historyQty.push(item.qty);
}
return list;
}
function randomAlgo(day) {
let quart = Math.floor(Math.random() * 4);
let multiple = Math.floor(Math.random() * 10);
let maxQuart = Math.floor(Math.random() * (quart * multiple * day));
let minQuart = Math.floor(Math.random() * (quart * multiple));
return Math.floor(Math.random() * (maxQuart - minQuart)) + minQuart;
}
这是我的明天减速器:
import { ACTIONS } from '../utils/consts';
import { randomiseMarketList } from '../utils/helpers';
var initialState = {
currentPlaceList: []
};
export function tomorrow(state = initialState, action = '') {
switch (action.type) {
case ACTIONS.BUILD_TOMORROW_LIST:
console.log(action);
let listToRandom = action.list.slice();
let randomactionList = randomiseMarketList([...listToRandom], action.day);
console.log(randomactionList);
let newList = Object.assign({}, state, { currentPlaceList: randomactionList });
return newList;
default:
return state;
}
}
正如您在我的 ACTIONS.BUILD_TOMORROW_LIST 中看到的,我首先在控制台记录操作以检查值,然后在我记录随机列表后,谁总是具有相同的值。我不明白为什么如果我更改它们它们具有相同的值。
我以为是因为数组是完全一样的,我直接改变了状态(不明白为什么)。我将列表作为由值和扩展运算符构建的新数组传递,也不起作用。我试图创建这个数组的副本,也不起作用。
当我们单击“留在这里”操作时,明天的列表将用于替换当前市场的列表。
handleStayHere(e) {
const { dispatch, status, tomorrow, market } = this.props;
dispatch( MarketActions.changeWholeList(tomorrow.currentPlaceList) );
dispatch( StatusActions.changeDay( status.day + 1 ) );
dispatch( TomorrowActions.buildTomorrowList([...market], status.day + 1) );
}
MarketActions.changeWholeList 操作效果很好,但我无法正确存储明天的列表。
感谢您的帮助!