0

I'm trying to write a function to append an array with a for loop. The data I'm trying to append is in a JSON repsonse. However, I keep getting x10 Array[] in the web console instead of one array with all the data. When I run console.log(dates[0]) I get returned in the web console "undefined". This tells me the data isn't even making it into the array. When I run console.log(time) I return x10 peices of data from the JSON I want but of course its not in the array. Any ideas? Thanks.

function mostRecent(time) {
    var dates=[];
    for (var i = 0; i < time.length; i++) {
        dates.push(dates[i]);
    }
    return console.log(dates);
    }
1
  • 4
    It's dates.push(time[i]) Commented Nov 18, 2019 at 0:42

1 Answer 1

3

You are pushing dates[i] with every loop cycle. And since dates array keeps being empty, you are actually pushing undefined.

Just replace dates.push(dates[i]) with dates.push(time[i]).

Note: You should return dates instead of console.log.

Sign up to request clarification or add additional context in comments.

2 Comments

Hmmm...I'm still receiving 10 undefined repeats in the console when I return console.log(dates[0]).
@prime90 Just return dates instead of console.log

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.