在对其应用unshift之后,Array变为Integer

问题描述 投票:0回答:2

我有一个像这样的对象:

{
  "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
  }
}

我想要unshift到一个数组(它的克隆版本)像这样:

[{
  "adjustedRawUrl": "",
  "category": "others",
  "createdAt": 1514432540000,
  "cubemapReady": false,
  "desktopUrl": "https://im.ages.io/BGOLielt?cors&w=513",
  "floorplanRotation": 0,
  "index": 0,
  "is720": false,
  "isTopLogo": false,
  "mobileUrl": "https://im.ages.io/BGOLielt?cors&w=4096",
  "name": "b1a042ff6-0c75-4af2-a9da-1a16f333baee_p0",
  "objectId": "3c312986-0ef1-42fc-9068-46fc13a04b8f",
  "panoramaRotation": {
    "x": 0,
    "y": 0,
    "z": 0
  },
  "position": {
    "x": 0,
    "y": 0
  },
  "rawUrl": "https://firebasestorage.googleapis.com/v0/b/vrcam-dev-5a815.appspot.com/o/5ab4f2a0-88e9-4bf5-86b5-61b528be707f/panoramas/panorama_lPea0mIc6H.png?alt=media&token=9e1494ff-2525-42a6-a058-12c26560349a",
  "stereoUrl": "",
  "thumbnail": "https://im.ages.io/BGOLielt?cors&w=400",
  "updatedAt": 1514432619000
}, {
  "adjustedRawUrl": "",
  "category": "others",
  "createdAt": 1514432231000,
  "cubemapReady": false,
  "desktopUrl": "https://im.ages.io/FK9uiel2?cors&w=251",
  "floorplanRotation": 0,
  "index": 1,
  "is720": false,
  "isTopLogo": false,
  "mobileUrl": "https://im.ages.io/FK9uiel2?cors&w=4096",
  "name": "b1a042ff6-0c75-4af2-a9da-1a16f333baee_p0",
  "objectId": "08e9197c-ab27-48d8-a48d-b33452fcfd11",
  "panoramaRotation": {
    "x": 0,
    "y": 0,
    "z": 0
  },
  "position": {
    "x": 0,
    "y": 0
  },
  "rawUrl": "https://firebasestorage.googleapis.com/v0/b/vrcam-dev-5a815.appspot.com/o/5ab4f2a0-88e9-4bf5-86b5-61b528be707f/panoramas/panorama_pMUnnhYmpH.png?alt=media&token=e422e4f1-389b-43b7-927b-022b31e37d61",
  "stereoUrl": "",
  "thumbnail": "https://im.ages.io/FK9uiel2?cors&w=400",
  "updatedAt": 1514432619000
}]

这就是我这样做的方式:

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

令我震惊的是,结果不是数组而是整数:3

为什么是这样?以及如何解决它?

javascript arrays
2个回答
7
投票

array.unshift()返回修改后的数组的length。它不会创建新数组而是修改现有数组。因此,您无需在此处使用任何作业。

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

1
投票

docs解释

unshift()方法将一个或多个元素添加到数组的开头并返回数组的新长度。

也许不是分配unshift的结果,而是

const newClonedArray = JSON.parse(JSON.stringify(array));
newClonedArray.unshift(object);
© www.soinside.com 2019 - 2024. All rights reserved.