我有一个像这样的对象:
{
"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
。
这是为什么?以及如何解决它?
答案 0 :(得分:7)
document.getElementsByName("example_length")[0].value=100
返回已修改数组的array.unshift()
。它不会创建新数组而是修改现有数组。因此,您无需在此处使用任何作业。
length

答案 1 :(得分:1)
docs解释
unshift()
方法将一个或多个元素添加到数组的开头,返回数组的新长度。
也许不是分配unshift
的结果,而是
const newClonedArray = JSON.parse(JSON.stringify(array));
newClonedArray.unshift(object);