I'm looping through an array of objects called $scope.points
which looks like this:
[
{"x": 200, "y": 200, "index": 0}
{"x": 200, "y": 200, "index": 1}
{"x": 200, "y": 200, "index": 2}
]
This is the JS, which loops through $scope.points
and saves them using Parse:
var createPanodatas = function() {
console.log('POINTS:', $scope.points)
_.each($scope.points, function(point) {
console.log('INDEX:', point.index)
var Panodata = AV.Object.extend('PanoramaData'),
panodata = new Panodata()
var json = {
'index': point.index,
'x': point.x,
'y': point.y,
'roomModelId': $scope.pano.id
}
panodata.save(json, {
success: function(panodata) {
console.log('Panodata saved.')
},
error: function(panodata, error) {
console.log('Failed to create new pano object, with error message: ' + error.message)
}
})
})
}
The saved items are saved ordered by date but with random index:
[
{
"objectId": "56a1e3dc7db2a2005a15533a",
"index": 1,
"roomModelId": "56a1e3d72e958a00515cfe3e",
"createdAt": "2016-01-22T08:10:04.646Z"
},
{
"objectId": "56a1e3dcc24aa8005415772f",
"index": 0,
"roomModelId": "56a1e3d72e958a00515cfe3e",
"createdAt": "2016-01-22T08:10:04.646Z"
},
{
"objectId": "56a1e3dc816dfa005919182b",
"index": 2,
"roomModelId": "56a1e3d72e958a00515cfe3e",
"createdAt": "2016-01-22T08:10:04.670Z"
},
]
Which confused me since console.log('POINTS:', $scope.points)
outputs the items in correct index order:
And console.log('INDEX:', point.index)
too:
INDEX: 0
INDEX: 1
INDEX: 2
Why are the items saved with random index? How to save the index in order? (e.g. 0
, 1
, 2
...)?