Skip to content Skip to sidebar Skip to footer

Array Becomes An Integer After Applying Unshift On It

I have an Object like this: { 'index': 0, 'name': 'b1a042ff6-0c75-4af2-a9da-1a16f333baee_p0', 'category': 'others', 'rawUrl': 'https://firebasestorage.googleapis.com/v0/b/v

Solution 1:

array.unshift() returns length of the modified array. It does not create a new array instead modifies the existing array. Thus you do not need to use any assignment here.

var object ={
  "index": 0,
  "name": "b1a042ff6-0c75-4af2-a9da-1a16f333baee_p0",
  "category": "others",
  "rawUrl": "https://firebasestorage.googleapis.com/v0/b/vrcam-dev-5a815.appspot.com/o/5ab4f2a0-88e9-4bf5-86b5-61b528be707f/panoramas/panorama_XTsagLoxbA.png?alt=media&token=68ef261e-0c5e-4bf0-aebc-ab845fcec01a",
  "isTopLogo": false,
  "origin": "https://firebasestorage.googleapis.com/v0/b/vrcam-dev-5a815.appspot.com/o/5ab4f2a0-88e9-4bf5-86b5-61b528be707f/panoramas/panorama_XTsagLoxbA.png?alt=media&token=68ef261e-0c5e-4bf0-aebc-ab845fcec01a",
  "position": {
    "x": 0,
    "y": 0
  },
  "panoramaRotation": {
    "x": 0,
    "y": 0,
    "z": 0
  }
}

var array = [];

array.unshift(object);

console.log(array);

Solution 2:

The docs explain

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

Maybe instead of assigning the result of unshift, do

const newClonedArray = JSON.parse(JSON.stringify(array));
newClonedArray.unshift(object);

Post a Comment for "Array Becomes An Integer After Applying Unshift On It"