0

I would like to know:

  1. When do you use the indexing method to assign values to a new array?
  2. and; When do you use the push method?

I know you can assign a value to an array in a for loop by using indexing.

My question is:
I have a for loop that looks like this:

for (i=array_len-2;i>0;i-=2)
{
var new_arry=array[i]
}

... that would just return the value for i. It does not store it in the new array.

but, if i do that same thing:

for (i=array_len-2;i>0;i-=2)
{
var new_arry=array.push(i)
}

it adds the new element in the array in the format of [element one , element two].

zer00ne
  • 41,936
  • 6
  • 41
  • 68
  • 1
    you don't need to instantiate `new_arry` inside the for loop – Dhaval Jardosh Nov 08 '17 at 17:12
  • 1
    If you want to make a new array, neither of those code blocks will work. – Mike Cluck Nov 08 '17 at 17:12
  • The first one continually overwrites new_array with whatever is in array[i], the second one continually overwrites new_array with the return value of push(). Neither make a new array, or set an array element – Patrick Evans Nov 08 '17 at 17:13
  • A key thing to note about push vs explicit set can be seen by the difference in this output: `let a = []; a["foo"] = "bar"; console.log(a.length);`. `length` is still 0. – zero298 Nov 08 '17 at 17:13
  • 1
    What is the question? Adding values from one array to another? – Kiogara Nov 08 '17 at 17:28
  • I have tried formatting your post but your intention was not clear? What are you trying to achieve? – Nik Nov 08 '17 at 17:33
  • I want new array to story every two indexs of array. for (i=array_len-2;i>0;i-=2) new_arry=array[i] that only stores the indexs of array where i is – Daniel Anderson Nov 08 '17 at 18:49

3 Answers3

1

The following example shows, how you should use the index i and the push method.

var myArray = ["hello", "world", "welcome", "javascript", "user"];
var newArray = new Array();

for(var i=myArray.length-2; i>0; i-=2) {
  newArray.push(myArray[i]);
}
console.log(newArray);

A push() simply add anything you pass as argument to an array. Index i are used to identify the "index location" on an array.

Nik
  • 709
  • 4
  • 22
Chandru
  • 10,864
  • 6
  • 38
  • 53
1

Either way works, just create an empty array outside the loop. Use the incrementing variable as you progress through each iteration (loop). Avoid using keyword new when making arrays, it's 99% better to use literal ex. var array = [];

Keep in mind that the loop in your examples go backward (-=2) and it's unusual in that that it stops early (i=array_len-2;i>0;), normally a loop is set at full length (i=array_len which is derived from var array_len = array.length most likely). So this unorthodox loop will only add 2 extra elements if the array is only 4 to 5 in length (I might be off...).

Demo

// Fill array by push()
var arr1 = [];

for (let i=0; i < 5; i++) {
  arr1.push(i);
}

console.log(arr1);

// Fill array by assignment
var arr2 = [];

for (let i=0; i< 5; i++) {
  arr2[i] = i;
}

console.log(arr2);
Community
  • 1
  • 1
zer00ne
  • 41,936
  • 6
  • 41
  • 68
0

You'll want to use indexes for anything that should be in a specific index if it doesn't matter you can probably use push() which will return the new length and it is said on this other post that is faster than indexes.

Kiogara
  • 617
  • 1
  • 6
  • 15