7

I have a 2 dimensional array called vArr. It looks like follows...

vArr = [[1, 1], [2, 2], [3, 3]];

What I am trying to do, is move all the array elements along when a new array needs to be appended to the beginning of vArr, and then delete the last element of vArr to preserve its size (in this case 3). So for example, if a new element [4, 4] comes along that I want to append to vArr, the new vArr should look like this..

vArr = [[4, 4], [1, 1], [2, 2]];

[4, 4] has been appended to the beginning, all the other elements have moved along and any remaining elements past the vArr size of 3 (namely [3, 3]) have been removed. Here is my code so far...

var vArr = [[1, 1], [2, 3], [3, 3]];
var newv = [4, 4]; // New array to append

// My attempt at splicing newv to position 0 in vArr array without removing anything
vArr = vArr.splice(0, 0, newv);

// newv logs to the console successfully, however vArr is undefined for some reason
console.log(newv);
console.log(vArr);

// Get rid of final element (doesn't work because vArr is no longer defined from above)
vArr = vArr.pop();

I know there is probably something wrong with the syntax in the splicing line, but I have been unable to find any information online about what is wrong here. Does the splice method have to change if you are splicing arrays into 2D arrays? Or does it not work at all in this instance? Any help is greatly appreciated!

  • REOPEN REVIEW: "JUNE 5th, 2022 @ 22:28 UTC" Id argue that the duplicate is the following: https://stackoverflow.com/questions/1590247/how-do-you-implement-a-stack-and-a-queue-in-javascript The question doesn't use the word Queue, but that is what the user is asking for. The fact that the example is demonstrated using a multi-dimensional array is irrelevant. The correct solution's logic, would be the same regardless if the example used a one dimensional or two dimensional array. The correct solution is to demonstrate how to create a queue in JavaScript, which is shown in the link I shared above – JΛYDΞV Jun 05 '22 at 22:32

3 Answers3

1

Here you go with the solution https://jsfiddle.net/fkz9ubug/

var vArr = [[1, 1], [2, 3], [3, 3]];
var newv = [4, 4];
vArr.unshift(newv)

console.log(vArr);

Documentation

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
  • Brilliant! Thanks a lot! –  Jul 11 '17 at 07:28
  • Although this is the correct way of doing this, I'd like to point out that what the OP was trying should also work, but he made an error in assigning the value. So this is basically an alternative (better) solution, not an answer to his exact problem ;) The next time he uses `splice()` he'll make the same error. – vi5ion Jul 11 '17 at 07:39
0

The problem is with assigning the result of vArr.splice(0, 0, newv) back to vArr.

The splice function can also remove items from the array and the return value of splice() is those removed items.

So vArr = vArr.splice(0, 0, newv); should simply be vArr.splice(0, 0, newv);.

vi5ion
  • 1,028
  • 7
  • 11
0

You can use unshift() for pushing the new array at the beginning of the original array, and pop() to remove the last element from the array:

var vArr = [[1, 1], [2, 2], [3, 3]];
var arrToPush = [4, 4];
vArr.unshift(arrToPush);
vArr.pop();
console.log(vArr);
Ionut Necula
  • 11,107
  • 4
  • 45
  • 69