Jenan
Jenan

Reputation: 3320

How to delete data from the javascript object from this data structure?

I need to read all the connection times. (connectionTimes) I need to delete the line - when it will be offline more than online:

    userId: 1,
    connectionTimes:
        [
            {onlineTime:"11:10:30", offlineTime:"11:18:12"}, //delete
            {onlineTime:"11:14:14", offlineTime:"11:52:41"}  //delete
        ]

Delete user id - When times the connection will be empty.

 userId: 1, //delete userid
        connectionTimes:
            [
                //empty connection
            ]

I have this data structure:

var users = [];

    users[0] = {
        userId: 1,
        connectionTimes:
            [
                {onlineTime:"11:10:30", offlineTime:"11:18:12"},
                {onlineTime:"11:14:14", offlineTime:"11:52:41"}
            ]
    }

    users[1] = {
        userId: 2,
        connectionTimes: 
            [
                {onlineTime:"8:08:14", offlineTime:"1:15:00"}
            ]
    } 

Upvotes: 2

Views: 19330

Answers (4)

hugomg
hugomg

Reputation: 69934

There are many ways to remove things from arrays in Javascript. The mains ones I can think of are

  • The delete operator. Using it sets the chosen position to undefined and is similar to just setting the element to null. (The main difference is that deleting a key will cause it to be skiped when iterating with the forEach and map array methods).

    var xs = [0,1,2];
    delete xs[1];
    console.log(xs); // [0, undefined, 2]
    
  • The splice method can remove a chunk or the array, moving the remaining elements left to fill it in.

    var xs = [0,1,2,3];
    xs.splice(2, 1); //index, ammount to remove
    console.log(xs); // [0,1,3]
    
  • Setting the array's length property truncates it. This can be used to manually remove elements the old fashioned way when you want more control.

    var xs = [0,1,2,3];
    xs.length = 2;
    console.log(xs); // [0,1]
    xs.length = 4;
    console.log(xs); // [0,1, undefined, undefined]
    

So in your case we might do something like this:

function filter_in_place(array, predicate){
    var j=0;
    for(var i=0; i<arr.length; i++){
        var x = arr[i];
        if(pred(x)){
            arr[j++] = x;
        }
    }
    arr.length = j;
}

for(var i=0; i < users.length; i++){
    filter_in_place( users[i].connections, function(conn){
        /*return if offline larger then online*/
    });
}

filter_in_place(users, function(user){ return user.connections.length > 0; });

Upvotes: 2

Andreas Koch
Andreas Koch

Reputation: 2037

You can delete a property from an JavaScript object with the delete operator:

var sampleObject = {
    "key1": "value1",
    "key2": "value"
};
delete sampleObject["key2"];

or like this:

delete sampleObject.key2

See the Mozilla Developer Network JavaScript Reference for more background on the delete operator: https://developer.mozilla.org/en/JavaScript/Reference/Operators/delete

Your specific example would look something like this:

for(var id in users) {
    var user = users[id];
    if (user.connectionTimes.length === 0) {
        delete users[id];
        break
    }

    for(var i=0; i<=user.connectionTimes.length; i++) {
        var connectionTime = user.connectionTimes[i];
        if (connectionTime.onlineTime < connectionTime.offlineTime) {
            delete users[id];
            break;
        }
    }
}

Here is a link to jsFiddle showing the code in action: http://jsfiddle.net/Q86Jd/

Upvotes: 9

Jordan Running
Jordan Running

Reputation: 106027

This is pretty straightforward. In pseudocode:

declare user, times

for each index in users
  set user  = users[ index ]
  set times = user.connectionTimes

  if times is empty
    then delete users[ index ]

  else
    declare onlineSecs, offlineSecs

    for each index2 in times
      set onlineSecs  = timeInSeconds( times[ index2 ].onlineTime )
      set offlineSecs = timeInSeconds( times[ index2 ].offlineTime )

      if offlineSecs > onlineSecs
        then delete times[ index2 ]

In JavaScript the command to delete a variable is delete and can be used on arrays the same, e.g. delete someArray[ someIdx ];. The implementation of timeInSeconds will be something like this:

function timeInSeconds( timeWithColons ) {
  var hrsMinsSecs = "12:34:56".split(':');

  return ( parseInt( hrsMinsSecs[0] ) * 60 +
           parseInt( hrsMinsSecs[1] )
         ) * 60 +
         parseInt( hrsMinsSecs[2] )
  ;
}

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

Basically you want to use something like this:

for( var i=0; i<users.length; i++) {
    for( var j=0; j<users[i].connectionTimes.length; j++) {
        if( users[i].connectionTimes[j].onlineTime < users[i].connectionTimes[j].offlineTime) {
            delete users[i].connectionTimes[j];
            j--;
        }
    }
    if( users[i].connectionTimes.length == 0) {
        delete users[i];
        i--;
    }
}

Upvotes: 1

Related Questions