Javascript React Native,如何将嵌套的if-else转换为switch语句?
Hye,我是react redux的新手,我正在努力将嵌套的if-else语句转换为switch语句。谁能告诉我怎么做?下面是我的代码Javascript React Native,如何将嵌套的if-else转换为switch语句?,javascript,reactjs,react-native,react-redux,Javascript,Reactjs,React Native,React Redux,Hye,我是react redux的新手,我正在努力将嵌套的if-else语句转换为switch语句。谁能告诉我怎么做?下面是我的代码 if (action.type === ADD_TO_CART) { let addedItem = state.jeans.find((item) => item.id === action.id); let existed_item = state.addedItems.find((item) => action.id === i
if (action.type === ADD_TO_CART) {
let addedItem = state.jeans.find((item) => item.id === action.id);
let existed_item = state.addedItems.find((item) => action.id === item.id);
if (existed_item) {
addedItem.quantity += 1;
return {
...state.jeans,
total: state.total + addedItem.price,
};
} else {
addedItem.quantity = 1;
let newTotal = state.total + addedItem.price;
return {
...state,
addedItems: [...state.addedItems, addedItem],
total: newTotal,
};
}
}
你的先生是对的。它应该使用switch编写,因为动作类型列表很可能会增加,并且您将有多个案例 应该是这样的:
我猜你只是想把它从if else切换到switch对吗 在这种情况下,我想你可以做到以下几点:
if (action.type === ADD_TO_CART) {
let addedItem = state.jeans.find((item) => item.id === action.id);
let existed_item = state.addedItems.find((item) => action.id === item.id);
switch(existed_item){
case !undefined: {
addedItem.quantity += 1;
return {
...state.jeans,
total: state.total + addedItem.price,
};
}
default: {
addedItem.quantity = 1;
let newTotal = state.total + addedItem.price;
return {
...state,
addedItems: [...state.addedItems, addedItem],
total: newTotal,
};
}
}
}
这边
switch (action.type) {
case ADD_TO_CART :
let addedItem = state.jeans.find((item) => item.id === action.id);
let existed_item = state.addedItems.find((item) => action.id === item.id);
const itemExists = existed_item ? true : false;
switch (existed_item) {
case true :
addedItem.quantity += 1;
return {
...state.jeans,
total: state.total + addedItem.price,
};
case false :
addedItem.quantity = 1;
let newTotal = state.total + addedItem.price;
return {
...state,
addedItems: [...state.addedItems, addedItem],
total: newTotal,
};
}
}
试试这个
switch(existed_item) {
case true:
addedItem.quantity += 1;
return {
...state.jeans,
total: state.total + addedItem.price,
};
case false:
addedItem.quantity = 1;
let newTotal = state.total + addedItem.price;
return {
...state,
addedItems: [...state.addedItems, addedItem],
total: newTotal,
};
default:
return null;
}
你为什么要这么做?您对代码的现状有什么问题?我没有任何问题,但我的先生说我需要将其写入交换机。。你能告诉我怎么做吗?对于
if(action.type==ADD\u-to\u-CART)
isSwitch(action.type){case ADD\u-to\u-CART{
所以基本上对于嵌套的if-else我们需要嵌套的switch语句,对吗?是的,没错,我以为你的意思只是为了内部,但如果你想全部作为switch,你的方法是正确的:)Thnx,但我是老用户有人否决了我的4-5个问题,我已经将其标记为答案,所以他们阻止了我的帐户提问questions@KanwarjeetSingh是的,我对你来说,事实上我也有同样的问题:)我的1-2个账户因为同样的原因被冻结了,因为我不知道如何巧妙地提问。。
switch(existed_item) {
case true:
addedItem.quantity += 1;
return {
...state.jeans,
total: state.total + addedItem.price,
};
case false:
addedItem.quantity = 1;
let newTotal = state.total + addedItem.price;
return {
...state,
addedItems: [...state.addedItems, addedItem],
total: newTotal,
};
default:
return null;
}