简体   繁体   English

我该如何解决:未捕获的TypeError:无法读取JavaScript中未定义的属性'toString'

[英]how can i fix this: Uncaught TypeError: Cannot read property 'toString' of undefined in javascript

trying to write a function that takes a string, and returns the string back with first letter of each word in capital. 尝试编写一个函数,该函数需要一个字符串,然后将字符串返回,并以大写形式包含每个单词的第一个字母。 i am able to do so. 我能够做到。 But i keep on getting undefined value at my first index value in the iteration, tho the i have items in my list. 但是我一直在迭代中获得第一个索引值的不确定值,因为我的列表中有项目。

i wrote my function that takes a string parameter. 我写了一个带有字符串参数的函数。 i pass my parameter in to split using split(" ") method it so i can get an array of words. 我使用split(“”)方法将参数传递给split,这样我就可以得到一个单词数组。 and when i created my forloop to go over all of them to be able to make each of their first letters Capital. 当我创建我的forloop遍历所有这些对象时,可以使每个字母的首字母大写。 now in the iteration, i create a variable to store the first index element converted into a string using the toString() method so i can later apply the toUpperCase() method on it 现在在迭代中,我创建了一个变量来存储使用toString()方法转换为字符串的第一个索引元素,以便稍后可以在其上应用toUpperCase()方法

When i do a console.log() in the for loop i can see the elements converted all listed out (when i execute the function and pass a string to it) 当我在for循环中执行console.log()时,我可以看到所有列出的元素都已转换(当我执行该函数并将一个字符串传递给它时)

But i do not understand why i get: Uncaught TypeError: Cannot read property 'toString' of undefined...and this error points to the variable holding the element at index [i] when i am converting it to string in the forloop: var pickString = newStringList[i].toString() 但是我不明白为什么会得到:Uncaught TypeError:无法读取未定义的属性'toString'...并且当我在forloop中将其转换为字符串时,此错误指向变量将索引[i]上的元素保留pickString = newStringList [i] .toString()

function capitalizeLertters (letter) {
    var newStringList = letter.split(" ");
    // console.log(newStringList[0])
    var addAll = []

    for (var i = 0; i <= newStringList.length; i++) {
        console.log(newStringList[i])
        var pickString = newStringList[i].toString()
        // console.log(pickString)
        var finalString = pickString.charAt(0).toUpperCase() + pickString.slice(1)
        // console.log(finalString)
    }
    addAll.push(finalString)
    // console.log(addAll)
    return addAll
}

console.log(capitalizeLertters("js string exercises"))

This is my expected result: "Js String Exercises" 这是我的预期结果:“ Js弦乐练习”

And these are the different console.log results 这些是不同的console.log结果

""js Js string String exercises Exercises"" “” js Js字符串字符串练习练习“

But my function does not execute in the end...it throws this: 但是我的函数最终没有执行...它抛出了这个:

"""learnJs.js:100 Uncaught TypeError: Cannot read property 'toString' of undefined at capitalizeLertters (learnJs.js:100) at learnJs.js:110""" “”“ learnJs.js:100未捕获的TypeError:无法在learningJs.js:110处的capitalizeLertters(learnJs.js:100)读取未定义的属性'toString'

So 所以

  1. You don't need toString() on a String 您不需要在字符串上使用toString()

  2. The for loop crashes at the last element because you should stop at length -1 or simply replace <= with < . for循环在最后一个元素处崩溃,因为您应该在length -1处停止,或者只是将<=替换为<

  3. You are calling addAll after the loop, so it only adds the last element. 您在循环后调用addAll ,因此它仅添加最后一个元素。

  4. You are returning an array and I assume you want the String, so you can use join() on the array. 您正在返回一个数组,并且我假设您要使用String,因此可以在数组上使用join()

 function capitalizeLertters (letter) { var newStringList = letter.split(" "); var addAll = []; for (var i = 0; i < newStringList.length; i++) { var pickString = newStringList[i]; var finalString = pickString.charAt(0).toUpperCase() + pickString.slice(1); addAll.push(finalString); } return addAll.join(" "); } console.log(capitalizeLertters("js string exercises")) 

please check the modifications 请检查修改

  • push() method should be inside the loop. push()方法应该在循环内部。
  • you are looping from index 0, thats why you need to use < instead of <= 您从索引0开始循环,这就是为什么您需要使用<而不是<=
  • finally need to use join method to get string from array 最后需要使用join方法从数组中获取字符串

Solution : 解决方案

function capitalizeLertters (letter) {
    var newStringList = letter.split(" ");
    // console.log(newStringList[0])
    var addAll = [];

    for (var i = 0; i < newStringList.length; i++) {
        console.log(newStringList[i]);
        var pickString = newStringList[i];
        // console.log(pickString)
        var finalString = pickString.charAt(0).toUpperCase() + pickString.slice(1);
        // console.log(finalString)
        addAll.push(finalString);
    }
    // console.log(addAll)
    return addAll.join(" ");
}

capitalizeLertters("js string exercises");

An array starts with the index number 0. The length of newStringList is 3. If you give a less than (<) then 'i' will go like 0("js"), 1("string"), 2("exercises"). 数组以索引号0开头。newStringList的长度为3。如果给出的长度小于(<),则'i'会像0(“ js”),1(“ string”),2(“ exercises ”)。 If you give a less than equal (<=) it will go like 0("js"), 1("string"), 2("exercises"), 3(undefined). 如果给出的值小于等于(<=),它将像0(“ js”),1(“ string”),2(“ exercises”),3(undefined)一样。

  function capitalizeLertters (letter) {
  var newStringList = letter.split(" ");

  var addAll = ""

  for (var i = 0; i < newStringList.length; i++) {

  var pickString = newStringList[i].toString()

  var finalString = pickString.charAt(0).toUpperCase() + pickString.slice(1)
  addAll += finalString
  if(i != newStringList.length-1){
    addAll +=" "
  }
}


return addAll
}

console.log(capitalizeLertters("js string exercises"))

暂无
暂无

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

相关问题 未捕获的TypeError:无法读取未定义JavaScript的属性&#39;toString&#39; - Uncaught TypeError: Cannot read property 'toString' of undefined JavaScript 未捕获的类型错误:无法读取未定义的属性“toString”--javascript - Uncaught TypeError: Cannot read property 'toString' of undefined --javascript 未捕获的TypeError:无法读取未定义的属性&#39;toString&#39; - Uncaught TypeError: Cannot read property 'toString' of undefined 我该如何解决-未捕获的TypeError:无法读取未定义的属性&#39;scrollHeight&#39; - How can I fix - Uncaught TypeError: Cannot read property 'scrollHeight' of undefined 错误:未捕获TypeError:无法读取未定义的属性“ city”,并且如何在JavaScript中的对象键中包含破折号? - Error : Uncaught TypeError: Cannot read property 'city' of undefined and How can I include a dash in an object key in javascript? 如何修复未捕获的类型错误:无法读取未定义的属性“名称” - How to fix Uncaught TypeError: Cannot read property 'name' of undefined 如何解决“未捕获的TypeError:无法读取未定义的属性&#39;Timestamp&#39;”? - How to fix “Uncaught TypeError: Cannot read property 'Timestamp' of undefined”? 如何修复Uncaught TypeError:无法读取undefined的属性&#39;settings&#39; - How to fix Uncaught TypeError: Cannot read property 'settings' of undefined 如何修复未捕获的类型错误:无法读取未定义的属性“原型”? - How to fix Uncaught TypeError: Cannot read property 'prototype' of undefined? 如何修复“未捕获的类型错误:无法读取未定义的属性‘替换’”错误? - How to fix "Uncaught TypeError: Cannot read property 'replace' of undefined" error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM