Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

push() method not working properly in JavaScript

I'm trying to write a quite simple program that divides an array in another array of defined size smaller arrays, however the push() method is not working. Could someone please help me with it?

function chunk(array, size) {
  var newArray = [];
  var tempArray = [];

  for (let i = 0; i < array.length / size; i++) {
    for (let j = size * i, k = 0; j < size * i + size; j++, k++)
      tempArray[k] = array[j];

    newArray.push(tempArray);
  }

  return newArray;
}

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

console.log(chunk(data, 2));

The ideal output should be [[1, 2],[3, 4], [5, 6], [7, 8]]. However im getting [[7,8],[7,8],[7,8],[7,8]].

like image 846
Alonso Marañón Avatar asked Aug 03 '21 06:08

Alonso Marañón


People also ask

How push method works in JavaScript?

push() The push() method adds one or more elements to the end of an array and returns the new length of the array.

What can I use instead of push in JavaScript?

Alternatives of push() method in JavaScript Approach 1: Use the length property to insert the element at the end of the array. Example: This example implements the above approach.

What is the opposite of push () in JavaScript?

The opposite of push() (as the question is titled) is pop() .

Can push () be used on string?

Because strings are immutable (cannot be modified) any method which attempts to change the string will fail. That immediately rules out methods like push , pop , shift , and splice .


3 Answers

You're almost there. Just move the tempArray definition inside your first for-loop. Otherwise you would be pushing the same array each time.

Working Example:

function chunk(array, size) {
  const newArray = [];

  for (let i = 0; i < array.length / size; i++) {
    const tempArray = [];
    for (let j = size * i, k = 0; j < size * i + size; j++, k++)
      tempArray[k] = array[j];

    newArray.push(tempArray);
  }

  return newArray;
};

const data = [1, 2, 3, 4, 5, 6, 7, 8];

console.log(chunk(data, 2)); // [[1, 2],[3, 4], [5, 6], [7, 8]]
like image 140
Behemoth Avatar answered Oct 08 '22 19:10

Behemoth


@Behemoth's answer is the correct one for the question. But if you want, you can take a slightly different approach like this to reach the solution as well.

function chunk(array, size){
    const newArray = [];
    let i,j;
    
    for (i = 0,j = array.length; i < j; i += size) {
      newArray.push(array.slice(i, i + size));
    }
    return newArray;
};

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

console.log(chunk(data, 2));
like image 4
Rukshan Avatar answered Oct 08 '22 19:10

Rukshan


Slightly different solution than @Behemoth and @Rukshan

function chunk(array, size) {
  var newArray = [];
  var tempArray = [];

  for (let i = 0; i < array.length / size; i++) {

    for (let j = i; j < i + 1; j++) {
      for (let k = 0; k < size; k++) {
        if (array[size * j + k]) {
          tempArray.push(array[size * j + k]);
        }
      }
      newArray.push(tempArray);
      tempArray = [];
    }
  }
  return newArray;
};

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

console.log(chunk(data, 2));

The solution would be [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9 ] ] instead of [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ]]

like image 3
Aniruddha Kanere Avatar answered Oct 08 '22 18:10

Aniruddha Kanere