Cannot Read Property Presentation of Undefined Angular

Cannot Read Property 'split' of Undefined

If you lot've always used JavaScript's carve up method, in that location's a good hazard that y'all've encountered the post-obit error: TypeError: Cannot read belongings 'split' of undefined.

There are a few reasons why y'all would receive this error. Most likely it'due south just a basic misunderstanding of how split works and how to iterate through arrays.

For example, if you lot try to submit the following lawmaking for the Detect the Longest Word in a String challenge:

                office findLongestWord(str) {    for(let i = 0; i < str.length; i++) {     const array = str.separate(" ");     array[i].split("");   } }  findLongestWord("The quick brown play tricks jumped over the lazy canis familiaris");              

it will throw the TypeError: Cannot read property 'split' of undefined error.

The split up method

When split is chosen on a string, it splits the string into substrings based on the separator passed in as an argument. If an empty string is passed equally an argument, divide treats each character as a substring. It then returns an array containing the substrings:

                const testStr1 = "Test test 1 ii"; const testStr2 = "cupcake pancake"; const testStr3 = "Offset,Second,Third";  testStr1.split(" "); // [ 'Exam', 'test', 'one', 'ii' ] testStr2.split(""); // [ 'c', 'u', 'p', 'c', 'a', 'chiliad', 'e', ' ', 'p', 'a', 'n', 'c', 'a', 'm', 'due east' ] testStr3.split(","); // [ 'Offset', 'Second', 'Tertiary' ]                              

Check out MDN for more details about divide.

The problem explained with examples

Knowing what the split method returns and how many substrings you tin can expect is the primal to solving this challenge.

Permit'due south take another look at the code higher up and see why it's not working as expected:

                part findLongestWord(str) {    for(permit i = 0; i < str.length; i++) {     const array = str.split(" ");     assortment[i].dissever("");   } }  findLongestWord("The quick brown play a trick on jumped over the lazy dog");                              

Splitting str into an array like this (const assortment = str.split(" ");) works equally expected and returns [ 'The',   'quick',   'chocolate-brown',   'fox',   'jumped',   'over',   'the',   'lazy',   'dog' ].

But take a closer wait at the for loop. Rather than using the length of array as a status to iterate i, str.length is used instead.

str is "The quick brown fox jumped over the lazy dog", and if you log str.length to the console, you lot'll get 44.

The last statement in the trunk of the for loop is what's causing the fault: array[i].split("");. The length of array is 9, so i would rapidly go way over the maximum length of array:

                function findLongestWord(str) {    for(let i = 0; i < str.length; i++) {     const array = str.carve up(" ");     console.log(array[i]);     // assortment[0]: "The"     // array[one]: "quick"     // array[ii]: "brown"     // ...     // array[nine]: "dog"     // assortment[x]: undefined     // array[xi]: undefined   } }  findLongestWord("The quick brown fox jumped over the lazy dog");                              

Calling assortment[i].split(""); to split each string into substrings of characters is a valid arroyo, but it volition throw TypeError: Cannot read belongings 'divide' of undefined when information technology'southward passed undefined.

How to solve Find the Longest Word in a String with split up

Allow's chop-chop get over some pseudo lawmaking for how to solve this problem:

  1. Split str into an assortment of private words
  2. Create a variable to runway the greatest word length
  3. Iterate through the array of words and compare the length of each word to the variable mentioned higher up
  4. If the length of the current word is greater than the one stored in the variable, supervene upon that value with the electric current word length
  5. In one case the length of every discussion is compared with the maximum word length variable, render that number from the function

Outset, separate str into an assortment of private words:

                part findLongestWordLength(str) {   const array = str.split(" "); }              

Create a variable to go on runway of the longest discussion length and gear up it to zero:

                part findLongestWordLength(str) {   const assortment = str.split(" ");   let maxWordLength = 0; }              

At present that the value of array is ['The', 'quick', 'chocolate-brown', 'fob', 'jumped', 'over', 'the', 'lazy', 'domestic dog'], you can utilize array.length in your for loop:

                role findLongestWordLength(str) {   const assortment = str.divide(" ");   let maxWordLength = 0;      for (let i = 0; i < array.length; i++) {        } }              

Iterate through the assortment of words and check the length of each give-and-take. Remember that strings besides have a length method you can call to easily get the length of a cord:

                function findLongestWordLength(str) {   const array = str.split(" ");   permit maxLength = 0;      for (let i = 0; i < array.length; i++) {     array[i].length;   } }              

Use an if argument cheque if the length of the current discussion (array[i].length) is greater than maxLength. If so, replace the value of maxLength with array[i].length:

                office findLongestWordLength(str) {   const array = str.separate(" ");   let maxLength = 0;      for (let i = 0; i < array.length; i++) {     if (array[i].length > maxLength) {       maxLength = array[i].length;     }   } }              

Finally, return maxLength at the end of the function, afterward the for loop:

                function findLongestWordLength(str) {   const array = str.separate(" ");   let maxLength = 0;      for (permit i = 0; i < array.length; i++) {     if (array[i].length > maxLength) {       maxLength = array[i].length;     }   }        return maxLength; }              

Learn to code for gratis. freeCodeCamp's open source curriculum has helped more than forty,000 people get jobs equally developers. Become started

harriscrinver1946.blogspot.com

Source: https://www.freecodecamp.org/news/cannot-read-property-split-of-undefined-error/

0 Response to "Cannot Read Property Presentation of Undefined Angular"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel