[英]Switch statement that maps through array gives unreachable code warning
this will probably be a very easy solution that I am missing, but hoping someone could help. 这可能是我很想念的一个非常简单的解决方案,但希望有人能提供帮助。
I am looping through an array and trying to figure out the length of it, and return different values depending on the length. 我正在遍历一个数组,试图找出它的长度,并根据长度返回不同的值。 Then I map through it and hopefully return the value depending on the amount.
然后,我映射它,并希望根据数量返回值。 Could someone tell me what I am doing wrong?
有人可以告诉我我在做什么错吗?
Thanks! 谢谢!
EDIT Thank you all for your comments. 编辑谢谢大家的评论。 What I intend to do with the switch statement result is determine a grid size depending on the getGridPercentages array value.
我打算对switch语句结果执行的操作是根据getGridPercentages数组值确定网格大小。 So if it is '4' it would return '25% 25% 25% 25%'.
因此,如果它是“ 4”,它将返回“ 25%25%25%25%”。
I am then using the value in a styled component using CSS grid's grid-template-columns: 然后,我使用CSS网格的grid-template-columns在样式化的组件中使用该值:
const ContainerWrapper = styled.div ` display: grid; grid-template-columns: ${op}; //Value of grid here grid-gap: 10px; `;
const getGridPercentages = [ { Text: '1' }, { Text: '2' }, { Text: '3' }, { Text: '4' } ] let op = getGridPercentages.map(function(item) { const getAmount = item.Text; switch(getAmount) { case '1': return '100%' case '2': return '50% 50%' case '3': return '33% 33% 33%' case '4': return '25% 25% 25% 25%' default: return 1 } });
You should make these changes 您应该进行这些更改
const getGridPercentages = [ { Text: '1' }, { Text: '2' }, { Text: '3' }, { Text: '4' } ] let op = getGridPercentages.map(function(item) { const getAmount = item.Text; switch(getAmount) { case '1': return '100%' case '2': return '50% 50%' case '3': return '33% 33% 33%' case '4': return '25% 25% 25% 25%' default: return 1 } }); console.log(op);
Update: As you asked in comments you want based on number of elements which is in complete contrary of what question is saying. 更新:正如您在评论中询问的那样,您希望基于元素的数量,这与所要表达的问题完全相反。
const getGridPercentages = [ { Text: '1' }, { Text: '2' }, { Text: '3' }, { Text: '4' } ] function getGridSize(arr){ switch(arr.length) { case 1: return '100%' case 2: return '50% 50%' case 3: return '33% 33% 33%' case 4: return '25% 25% 25% 25%' default: return 1 } } console.log(getGridSize(getGridPercentages))
You are using switch statement and when the condition match you are returning a value so there is no need for using break; 您使用的是switch语句,当条件匹配时,您将返回一个值,因此无需使用break; keyword after return.
返回后的关键字。 Your switch statement should looks like:
您的switch语句应如下所示:
switch(getAmount) {
case 1:
return '100%'
case 2:
return '50% 50%'
case 3:
return '33% 33% 33%'
case 4:
return '25% 25% 25% 25%'
default:
return 1
}
EDIT: and also you are getting the length of every Text string which will be in your case every time 1. 编辑:并且您也将获得每个文本字符串的长度,在您的情况下每次为1。
Two problems: 两个问题:
One, you don't need a break
statement after a return
. 第一,在
return
之后不需要break
语句。 That's what's triggering the error in the console. 这就是在控制台中触发错误的原因。
Two, you are switching on the length of each Text
field. 二,您正在打开每个
Text
字段的长度。 Text's length is always one. 文字的长度始终为1。 Therefore any other branches in your switch statement are also unreachable code.
因此,switch语句中的任何其他分支也是无法访问的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.