Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5992251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:30:17+00:00 2026-05-22T23:30:17+00:00

Why do nested for loops work in the way that they do in the

  • 0

Why do nested for loops work in the way that they do in the following example:

var times = [
            ["04/11/10", "86kg"], 
            ["05/12/11", "90kg"],
            ["06/12/11", "89kg"]
];

for (var i = 0; i < times.length; i++) {
        var newTimes = [];
        for(var x = 0; x < times[i].length; x++) {
            newTimes.push(times[i][x]);
            console.log(newTimes);  


        }

    }

In this example I would have thought console.log would give me the following output:

["04/11/10"]
["86kg"]
["05/12/11"]
["90kg"]
["06/12/11"]
["89kg"]

However, I actually get this:

["04/11/10"]
["04/11/10", "86kg"]
["05/12/11"]
["05/12/11", "90kg"]
["06/12/11"]
["06/12/11", "89kg"]

Is anyone able to help me understand this?

EDIT:

Thanks for all your responses!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:30:18+00:00Added an answer on May 22, 2026 at 11:30 pm

    You are redefining newTimes on every single loop and you are outputting to the console on each column push.

    var times = [
                ["04/11/10", "86kg"], 
                ["05/12/11", "90kg"],
                ["06/12/11", "89kg"]
    ];
     var newTimes = [];
    for (var i = 0; i < times.length; i++) {     
            for(var x = 0; x < times[i].length; x++) {
                newTimes.push(times[i][x]);
            }
        }
        console.log(newTimes);  
    

    Returns: ["04/11/10", "86kg", "05/12/11", "90kg", "06/12/11", "89kg"]
    http://jsfiddle.net/niklasvh/SuEdt/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the best way to break from nested loops in Javascript? //Write the links
What is the Big-O time complexity of the following nested loops: for (int i
I am programming an algorithm that contains 4 nested for loops. The problem is
I am trying to understand nested loops by getting this program to work. I'd
I want to create HTML nested lists that has the following format: 1 1.1
What if I have nested loops, and I want to break out of all
Suppose I need to break out of three or four nested for loops at
In runtime I'm creating a DataTable and using nested for-loops to populate the table.
I have nested xsl:for loops: <xsl:for-each select=/Root/A> <xsl:for-each select=/Root/B> <!-- Code --> </xsl:for> </xsl:for>
I'm sure most of you know that a nested loop has O(n^2) complexity if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.