Array becomes an Integer after applying unshift on it

前端 未结 2 1401
借酒劲吻你
借酒劲吻你 2021-01-23 02:47

I have an Object like this:

{
  \"index\": 0,
  \"name\": \"b1a042ff6-0c75-4af2-a9da-1a16f333baee_p0\",
  \"category\": \"others\",
  \"rawUrl\": \"https://fireb         


        
相关标签:
2条回答
  • 2021-01-23 03:09

    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);
    
    0 讨论(0)
  • 2021-01-23 03:16

    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);

    0 讨论(0)
提交回复
热议问题