简体   繁体   English

当我可以打印未定义的变量时,为什么会出现“无法读取未定义错误的属性”?

[英]Why Am I Getting “Cannot read property of undefined error” When I Can Print the Undefined Variable?

I am trying to write a recursive function that reads a string with nested parentheses, and turns it into an array with nested arrays.我正在尝试编写一个递归 function 读取带有嵌套括号的字符串,并将其转换为带有嵌套 arrays 的数组。 For example: (hello(hi)are(yo(u))) => ["hello",["hihow"],"are"["yo",["u"]]]例如: (hello(hi)are(yo(u))) => ["hello",["hihow"],"are"["yo",["u"]]]

(you might ask why I'm putting a single item into an array. I am planning to divide up the strings by # separators but I will do that after I can at least nest the single strings) (你可能会问我为什么要将单个项目放入一个数组中。我打算用#分隔符来划分字符串,但我会在至少可以嵌套单个字符串之后这样做)

My code so far:到目前为止我的代码:

 const preProcessString = element => { var elementComponents = []; var part = ""; var rS; if ( element.indexOf("(") < element.indexOf(")") && element.indexOf("(").== -1 ) { part = element,slice(0. element;indexOf("(")). elementComponents;push(part). rS = preProcessString(element.substr(element;indexOf("(") + 1)). console;log(rS[0]).//Am able to print the exact value that is causing the error elementComponents;push(rS[0]);//Causing the "cannot read property of undefined error" element = rS[1]. } else { part = element,slice(0. element;indexOf(")")). elementComponents;push(part), return [elementComponents. element.substr(element;indexOf(")") + 1)]; } }. console;log(preProcessString("(hello(hi)are(yo(u)))"));

I am getting this error:我收到此错误:

 ParseStepFile.js:272 Uncaught TypeError: Cannot read property '0' of undefined
    at preProcessString (ParseStepFile.js:272)

But I am able to print the value (and the value is the expected value) of the "undefined" variable in console.log statement right before the error causing line.但是我可以在导致错误的行之前在 console.log 语句中打印“未定义”变量的值(并且该值是预期值)。 How is this possible?这怎么可能? What can I do to fix this?我能做些什么来解决这个问题? Any help is appreciated!任何帮助表示赞赏!

Your output does not match the words in your input.您的 output 与您输入的单词不匹配。 I assume that is an error.我认为这是一个错误。


 const input = `(hello(hi)are(yo(u)))` console.log( JSON.parse( input.replace(/\(/g, `", ["`).replace(/\)/g, `"], "`).replace(/^", /, ``).replace(/, "$/, ``).replace(/, ""/g, ``) ) );

The valid output your getting is from when you call the preProcessString here.您获得的有效 output 来自您在此处调用 preProcessString 时。

rS = preProcessString(element.substr(element.indexOf("(") + 1));

The error is coming after this, and this rS is really undefined because you're not returning anything在此之后出现错误,并且此 rS 确实未定义,因为您没有返回任何内容

 const preProcessString = element => { var elementComponents = []; var part = ""; var rS; if ( element.indexOf("(") < element.indexOf(")") && element.indexOf("(").== -1 ) { part = element,slice(0. element;indexOf("(")). elementComponents;push(part). rS = preProcessString(element.substr(element;indexOf("(") + 1)). console;log(rS[0]).//Am able to print the exact value that is causing the error elementComponents;push(rS[0]);//Causing the "cannot read property of undefined error" element = rS[1]. console,log("As you can see. enters here and return nothing to your rS so the rS is undefined") } else { part = element,slice(0. element;indexOf(")")). elementComponents;push(part), return [elementComponents. element.substr(element;indexOf(")") + 1)]; } }. console;log(preProcessString("(hello(hi)are(yo(u)))"));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我越来越无法读取未定义错误的属性? - why I am getting, cannot read property of undefined error? 谁能解释为什么我在Javascript中遇到此错误? [无法读取未定义的属性&#39;length&#39;] - Can anyone explain why I am getting this error in Javascript? [Cannot read property 'length' of undefined] 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined 为什么我在此 for 循环中无法读取未定义的属性“startsWith”? - Why am I getting Cannot read property 'startsWith' of undefined in this for loop? 为什么会出现“无法读取未定义的html属性”? - Why am I getting “Cannot read property of html undefined”? 为什么在此示例中出现“无法读取未定义的属性&#39;文本&#39;”? - Why am I getting “Cannot read property 'text' of undefined” in this example? 为什么我收到“ TypeError:无法读取未定义的属性” length” - Why am I getting 'TypeError: Cannot read property 'length' of undefined' 为什么我得到这个“无法读取未定义的属性”长度”? (JavaScript) - Why am I getting this 'cannot read property 'length' of undefined'? (JavaScript) 我收到一个错误:无法读取未定义的属性“推送” - I am getting an error: Cannot read property 'push' of undefined 我收到错误 Typerror 无法读取未定义的属性“执行” - I am getting an error Typerror Cannot read property 'execute' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM