Javascript : unshift without modifing the array size

81
July 25, 2019, at 00:00 AM

I am generating an array like :

const myArray = Array.from({length: 5}, (e, i) => emptyX)

myArray starts with 5 elements of emptyX.

Is there a simple way/method which unshift the array without changing its size ? Whenever I call it I must find the initial size.

exemple:

myArray.unshift(X1) => [X1, emptyX, emptyX, emptyX, emptyX]
myArray.unshift(X2) => [X2, X1, emptyX, emptyX, emptyX]
myArray.unshift(X3) => [X3, X2, X1, emptyX, emptyX]
// OR with multiple parameters
myArray.unshift(X4, X5) => [X5, X4, X3, X2, X1]

UPDATE:

Now I am doing :

myArray.unshift(X);
myArray.splice(-1, 1);// or pop()

But it's not what I want, I need just to replace the items because the array size change when I call unshift and splice

Answer 1

You could do something like this-

class arrWrapper(myArray: Array) {
    this.arr = myArray;
    this.len = myArray.length;
    this.pos = 0;
    unshift(newObj) {
       //Add checks if lengeh > pos, do something - either return or pos = 0
       this.arr[this.pos++] = newObj;
    } 
}

After comments I realize what you tried to do. This will "shift" the items up, without removing them:

class arrWrapper(myArray: Array) {
    this.arr = myArray;
    unshift(newObj) {
       for (i=1; i<this.arr.length-1; i++) {
          this.arr[i] = this.arr[i-1];
       } 
       this.arr[0] = newObj;;
    } 
}

this will run on the array, shift the cells up, and always insert the newObj into the first position

Answer 2

You are looking for the ShiftRight method which is unavailable in the prototype. Here is a pollyfill:

Array.prototype.shiftRight = function(...params) { 
    
    params.forEach(item => { 
        for (var i = this.length - 1; i >= 0; i--) { 
            if (i === 0) 
               this[0] = item 
            else 
               this[i] = this[i - 1]; 
        } 
    }) 
} 
 
 
x = [1, 2, 3]; 
 
x.shiftRight(4, 5); 
 
console.log(x);   // [5, 4, 1]

Answer 3

Try this :

const myArray = Array.from({length: 5}, (e, i) => "emptyX") 
//with multiple parameters 
var items=[1,2,3,4]; 
 
doUnshift(myArray,items); 
 
 
function doUnshift(myArray,items){ 
  
    var l=myArray.length; 
    //myArray size must be greater than items array size 
    if(l<=myArray.length){ 
    items.reverse(); 
    for(var i=0;i<items.length;i++){ 
      myArray.unshift(items[i]) 
    } 
    myArray.splice(items.length,items.length) 
      console.log(myArray) 
   } else{ 
         console.log("myArray size must be greater than items array size")   
   } 
     
     
}

READ ALSO
Dropzone JS with tree explorer

Dropzone JS with tree explorer

Does there exist a library or extension to DropzoneJS where you can add a "folder view/tree list" of the media folder where the uploads are moved?

98
React Native: Take picture with camera, and crop it instantly on an specific area

React Native: Take picture with camera, and crop it instantly on an specific area

I wanted to crop an image exactly in the moment it was taken, not later, and i dont know if thats possible because all the libraries i have found, only do that after the picture was taken, and i want to crop it automatically on an specific area exactly...

96
Animated.Image is not showing on screen

Animated.Image is not showing on screen

I have a view where I display some information, and I want a photo that moveHow I can do to show the image and move indefinitely

73
Need to pair values of objects from array

Need to pair values of objects from array

I have an array of objects:

56