Json.parse Angular 2 Cannot Read of Undefined

Cannot Read Property 'split' of Undefined

If you've ever used JavaScript's split method, there'due south a good chance that you've encountered the following mistake: TypeError: Cannot read property 'split' of undefined.

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

For instance, if y'all try to submit the following code for the Find the Longest Give-and-take in a Cord challenge:

                function findLongestWord(str) {    for(let i = 0; i < str.length; i++) {     const array = str.split(" ");     array[i].split("");   } }  findLongestWord("The quick chocolate-brown pull a fast one on jumped over the lazy dog");              

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

The split method

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

                const testStr1 = "Test test ane 2"; const testStr2 = "cupcake pancake"; const testStr3 = "First,Second,Third";  testStr1.dissever(" "); // [ 'Test', 'test', '1', '2' ] testStr2.carve up(""); // [ 'c', 'u', 'p', 'c', 'a', 'k', 'e', ' ', 'p', 'a', 'n', 'c', 'a', 'k', 'e' ] testStr3.split(","); // [ 'First', 'Second', 'Third' ]                              

Check out MDN for more details well-nigh split.

The problem explained with examples

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

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

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

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

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

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

The last statement in the body of the for loop is what's causing the fault: array[i].carve up("");. The length of assortment is 9, so i would apace become way over the maximum length of array:

                part findLongestWord(str) {    for(let i = 0; i < str.length; i++) {     const array = str.split(" ");     console.log(array[i]);     // array[0]: "The"     // assortment[1]: "quick"     // array[2]: "brownish"     // ...     // array[nine]: "dog"     // assortment[10]: undefined     // array[11]: undefined   } }  findLongestWord("The quick brown fox jumped over the lazy dog");                              

Calling array[i].split(""); to split each string into substrings of characters is a valid approach, but it will throw TypeError: Cannot read property 'divide' of undefined when it's passed undefined.

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

Permit's apace go over some pseudo lawmaking for how to solve this trouble:

  1. Split str into an array of individual words
  2. Create a variable to rails the greatest discussion length
  3. Iterate through the array of words and compare the length of each word to the variable mentioned in a higher place
  4. If the length of the electric current give-and-take is greater than the one stored in the variable, replace that value with the electric current word length
  5. Once the length of every word is compared with the maximum word length variable, return that number from the function

First, split str into an array of individual words:

                function findLongestWordLength(str) {   const array = str.carve up(" "); }              

Create a variable to keep rails of the longest give-and-take length and set it to zilch:

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

Now that the value of array is ['The', 'quick', 'chocolate-brown', 'fox', 'jumped', 'over', 'the', 'lazy', 'dog'], you tin apply array.length in your for loop:

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

Iterate through the array of words and check the length of each word. Remember that strings also have a length method you can call to easily get the length of a cord:

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

Utilise an if statement bank check if the length of the electric current word (array[i].length) is greater than maxLength. If and so, replace the value of maxLength with array[i].length:

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

Finally, render maxLength at the cease of the function, after the for loop:

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

Larn to code for costless. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

medranowherrigh.blogspot.com

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

0 Response to "Json.parse Angular 2 Cannot Read of Undefined"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel