简体   繁体   English

通过数组映射的switch语句给出了无法到达的代码警告

[英]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 您应该进行这些更改

  1. item.Text.length will always return 1 as you're trying to find length of single character. 当您尝试查找单个字符的长度时,item.Text.length将始终返回1。
  2. No need of break statement after return statement.(This is what unreachable code looks like) return语句后无需break语句(这是无法访问的代码的样子)

 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.

相关问题 在switch语句中使用break时出现无法访问的代码错误 - Unreachable code error when I use a break in my switch statement 如何解决此“返回语句后无法到达的代码”警告? - How to resolve this “unreachable code after return statement” warning? 使用Switch语句遍历数组 - looping through an array with a Switch statement 警告:使用Reactjs无法访问代码 - Warning: Unreachable code , using Reactjs 通过Javascript中的switch()语句使用数组 - Using an array through a switch() statement in Javascript 根据ESLint,Switch语句默认返回“ Unreachable” - Switch Statement default return “Unreachable” according to ESLint 为什么 Javascript 中没有显示“无法访问”的语句错误/警告? - Why no "unreachable" statement error/warning shown in Javascript? 为什么在控制台中两次显示“返回语句后无法访问代码”警告? (火狐 72.0.2,Win 10) - Why is `unreachable code after return statement` warning shown twice in console? (Firefox 72.0.2,Win 10) 检测到javascript开关中断无法访问的代码 - javascript switch break unreachable code detected 用于…in语句的JavaScript在遍历数组时给出错误 - Javascript for…in statement gives error when looping through array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM