By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Trying to access a data point in a nested array within a while loop results in an undefined error
JavaScript

Javascript – Trying to access a data point in a nested array within a while loop results in an undefined error

Admin
Last updated: 2023/12/14 at 4:03 AM
Admin
Share
4 Min Read
Trying to access a data point in a nested array within a while loop results in an undefined error

Problem:

In the code below, I continue to get an error of TypeError: Cannot read properties of undefined (reading '0') for the conditional statement of the while loop.

Contents
Problem:Solution:
var testObj = {
    1: 1,
    2: 2,
    3: 3,
    4: 4,
    5: 5,
    6: 6,
    7: 7,
    8: 8,
    9: 9
};

var testArr = [
    [1, []],
    [2, []],
    [3, []],
    [4, []],
    [5, []],
    [6, []],
    [7, []],
    [8, []],
    [9, []]
];

(_ => {
    for (let x in testObj) {
        let i = 0;
        while (x !== testArr[i][0]) {
            i++;
        }
        testArr[i][1].push(x);
    }
})();

To explain how the code works, the anonymous function is supposed to iterate over each item in the object. During each iteration, the while loop will be used to find the array within the nested array for which the first data point (index 0) is the number that matches the current data point of the object, and then push that data point into the empty array located at index 1 in that array.

(This is a simplified version of a larger program for testing purposes. I am aware that the code is superfluous due to all numbers being ordered, but in the main program that won’t be the case.)

However, the program crashes upon entering the while loop due to the above mentioned type error.

I have confirmed that this is not a scope issue, as I can access the array from inside the while loop. I have also confirmed that a hard call to any of the data points in the nested array also works, so I don’t believe it is a syntax error. The only possible reason for the problem I can think of is that i is somehow not a number by the time it is used in the while loop, but I have no idea how that could be happening.

Solution:

  1. In your while loop, you are not checking for the condition when i exceeds the size of the array. It is looking for testArr[9] which will be undefined because there are only 8 elements in testArr.
  2. Why is it going to the 9th index you may ask- Because Keys in javascript objects can only be strings. Hence you are comparing string '1' with integer 1 which results in false and hence the loop keeps incrementing.

You can either change the !== to a != or convert the string keys to integer ones –

(_ => {
    for (let x in testObj) {
        let i = 0;
        while (x != testArr[i][0]) {
            i++;
        }
        testArr[i][1].push(x);
    }
})();
(_ => {
    for (let x in testObj) {
        // Convert string to int
        x = parseInt(x);
        let i = 0;
        while (x !== testArr[i][0]) {
            i++;
        }
        testArr[i][1].push(x);
    }
})();

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article How to add field to global object during test run? Javascript – How to add field to global object during test run?
Next Article Vue select option v-for value not sending any value Javascript – Vue select option v-for value not sending any value
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?