Skip to content Skip to sidebar Skip to footer

Looping Over An Array, And Pushing Data To Another Array

I'm writing a small script in JavaScript to output an array. I want to use the 'years' array, push the data into another array called new_array, and output it via console.log(). T

Solution 1:

You're over complicating it, you don't use both a for loop and pop unless your iterating from the end to the beginning

if you iterate from the beginning while popping the end, you will only get half the array:

 years = [1996, 1994, 1981, 1976];
        new_array = [];


        for(i = 0; i < years.length; i++) {
          new_array.push(years.pop());

        }
          console.log(new_array)

you have two options if you are modifying the original array

1) Use A While Loop

keep looping as long as there are still elements in the original array

years = [1996, 1994, 1981, 1976];
    new_array = [];
    
    
while(years.length) {
  new_array.push(years.pop());

}

  console.log(new_array)

2) Use a Decreasing For Loop

iterate from the end of the loop to the beginning

years = [1996, 1994, 1981, 1976];
new_array = [];


for(i = years.length - 1; i >= 0; i--) {
  new_array.push(years.pop());

}
  console.log(new_array)

Solution 2:

Maybe you really want to do this?

var years = [1996, 1994, 1981, 1976];
var reverse_years = years.slice().reverse(); // years.slice() creates new instance of Array so .reverse() does not affect original Arrayconsole.log(reverse_years);

If you don't care if the original Array is affected:

varyears= [1996, 1994, 1981, 1976];years.reverse();console.log(years);

Solution 3:

You don't say what the intention of the code is, but it has a few issues anyway:

for(i = 0; i <= years.length; i++) {
    popped_element = Object.values(years.pop([i]));

Object.values is experimental and not widely available, so you should not use that. pop doesn't take any arguments, so you pop the last value of years each time. And you're calling Object.values on the value returned by pop, which is a string that doesn't have any enumerable properties, hence the empty array.

If your intention is to return a copy of the array in reversed order, then copy the array and reverse it:

var years = [1996, 1994, 1981, 1976];

// Copy with slice, reverse with reversevar reversed = years.slice().reverse();
console.log(reversed)

Solution 4:

Just iterate and assign:

years= [1996, 1994, 1981, 1976];new_array= [];for(vari=0;i<years.length;i++) {
    new_array.push(years[i]);console.log(new_array);
}

then arrange elements in revere order (if that's what you wanted):

new_array.reverse();

and delete the items from years (again, if that's what you wanted):

years = [];

..or, just use slice to copy:

new_array = years.slice();

Solution 5:

Remove the call to Object.values() in the first line within the loop to solve the empty-array problem:

popped_element = years.pop();

From MDN:

The Object.values() method returns an array of a given object's own enumerable property values.

Your years have no enumerable property values, so it's returning an empty array and adding that empty array as an element in new_array.

The undefined at the end is due to the = in <= years.length. To fix, make that loop line:

for(i = 0; i < years.length; i++) {

or better yet, store the length before the loop and compare to that instead of accessing years.length on each loop iteration because pop() changes the array length.

Post a Comment for "Looping Over An Array, And Pushing Data To Another Array"