admin管理员组文章数量:1289659
I have an Object like this:
{
"index": 0,
"name": "b1a042ff6-0c75-4af2-a9da-1a16f333baee_p0",
"category": "others",
"rawUrl": ".appspot/o/5ab4f2a0-88e9-4bf5-86b5-61b528be707f/panoramas/panorama_XTsagLoxbA.png?alt=media&token=68ef261e-0c5e-4bf0-aebc-ab845fcec01a",
"isTopLogo": false,
"origin": ".appspot/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
}
}
Which I want to unshift
to an Array (it's cloned version) like this:
[{
"adjustedRawUrl": "",
"category": "others",
"createdAt": 1514432540000,
"cubemapReady": false,
"desktopUrl": ";w=513",
"floorplanRotation": 0,
"index": 0,
"is720": false,
"isTopLogo": false,
"mobileUrl": ";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": ".appspot/o/5ab4f2a0-88e9-4bf5-86b5-61b528be707f/panoramas/panorama_lPea0mIc6H.png?alt=media&token=9e1494ff-2525-42a6-a058-12c26560349a",
"stereoUrl": "",
"thumbnail": ";w=400",
"updatedAt": 1514432619000
}, {
"adjustedRawUrl": "",
"category": "others",
"createdAt": 1514432231000,
"cubemapReady": false,
"desktopUrl": ";w=251",
"floorplanRotation": 0,
"index": 1,
"is720": false,
"isTopLogo": false,
"mobileUrl": ";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": ".appspot/o/5ab4f2a0-88e9-4bf5-86b5-61b528be707f/panoramas/panorama_pMUnnhYmpH.png?alt=media&token=e422e4f1-389b-43b7-927b-022b31e37d61",
"stereoUrl": "",
"thumbnail": ";w=400",
"updatedAt": 1514432619000
}]
This is how I'm doing it:
const newClonedArray = JSON.parse(JSON.stringify(array)).unshift(object)
To my shock the result wasn't an Array but an Integer: 3
.
Why is this? And how to fix it?
I have an Object like this:
{
"index": 0,
"name": "b1a042ff6-0c75-4af2-a9da-1a16f333baee_p0",
"category": "others",
"rawUrl": "https://firebasestorage.googleapis./v0/b/vrcam-dev-5a815.appspot./o/5ab4f2a0-88e9-4bf5-86b5-61b528be707f/panoramas/panorama_XTsagLoxbA.png?alt=media&token=68ef261e-0c5e-4bf0-aebc-ab845fcec01a",
"isTopLogo": false,
"origin": "https://firebasestorage.googleapis./v0/b/vrcam-dev-5a815.appspot./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
}
}
Which I want to unshift
to an Array (it's cloned version) like this:
[{
"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./v0/b/vrcam-dev-5a815.appspot./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./v0/b/vrcam-dev-5a815.appspot./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
}]
This is how I'm doing it:
const newClonedArray = JSON.parse(JSON.stringify(array)).unshift(object)
To my shock the result wasn't an Array but an Integer: 3
.
Why is this? And how to fix it?
Share Improve this question asked Dec 28, 2017 at 3:47 alexalex 7,61115 gold badges53 silver badges79 bronze badges2 Answers
Reset to default 12array.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./v0/b/vrcam-dev-5a815.appspot./o/5ab4f2a0-88e9-4bf5-86b5-61b528be707f/panoramas/panorama_XTsagLoxbA.png?alt=media&token=68ef261e-0c5e-4bf0-aebc-ab845fcec01a",
"isTopLogo": false,
"origin": "https://firebasestorage.googleapis./v0/b/vrcam-dev-5a815.appspot./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);
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);
本文标签: javascriptArray becomes an Integer after applying unshift on itStack Overflow
版权声明:本文标题:javascript - Array becomes an Integer after applying unshift on it - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741426839a2378125.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论