Search code examples
javascriptarraysparse-platformparse-cloud-code

Parse - Array Value Deletion


I'm trying to run some cloud code in Parse that will allow me to check if my submissions from people are older than an hour, and if so, delete them.

In my swift file for the app I have:

for object in objects! {
    let newstring = NSString(format: "%.0f", self.slider.value)
    let newtimestamp = date
    object.addObject(newstring, forKey: "times")
    object.addObject(newtimestamp, forKey: "timestamp")
    object.saveInBackground()
}

And so I have two separate array's (times and timestamp) and I want to delete from both of them.

My JavaScript knowledge and how it works with Parse's cloud code isn't the strongest. I was thinking something along the lines of this logic, I just can't put it into the syntax / I'm not sure if it's able to be put in the syntax.

var old = true
var i = 0
var j = 0

while(old){
    if(currentTime >= timeStamp[i] + 3600) {
        i++
        j++
    }
    else {
        delete timeStamp [0,i--]
        delete times [0,j--]
        old = false
    }
}

That's my idea of the logic behind the delete function. I'm assuming I need a run background job for a one minute interval. I'm guessing it has something to do with a splice in JS, but not sure.

Any ideas on how to do this? Anything would be much appreciated.

Thank you


Solution

  • Deleting something in a javascript array is like this array.splice(index,1) deletes one element at index.

    for example:

    var arr = [1,2,3,4,5];
    arr.splice(2,1); //[3]
    console.log(arr); // [1,2,4,5];
    

    If you want to delete something at the end of an array:

    arr.pop(); //5
    console.log(arr); // [1,2,4];