如何从这个数据结构中删除javascript对象中的数据?

时间:2012-01-23 22:01:03

标签: javascript arrays

我需要阅读所有连接时间。 (connectionTimes) 我需要删除该行 - 当它离线时比在线更多:

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

删除用户ID - 当连接为空时。

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

我有这个数据结构:

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"}
            ]
    } 

4 个答案:

答案 0 :(得分:8)

您可以使用delete运算符从JavaScript对象中删除属性:

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

或者像这样:

delete sampleObject.key2

有关delete运算符的更多背景信息,请参阅Mozilla Developer Network JavaScript Reference: https://developer.mozilla.org/en/JavaScript/Reference/Operators/delete

您的具体示例如下所示:

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;
        }
    }
}

这是jsFiddle的链接,显示了代码的实际效果: http://jsfiddle.net/Q86Jd/

答案 1 :(得分:2)

有许多方法可以在Javascript中从数组中删除内容。我能想到的主要是

  • delete运算符。使用它将所选位置设置为undefined,类似于将元素设置为null。 (主要区别在于删除密钥会导致在使用forEach和map数组方法进行迭代时使用它。)

    var xs = [0,1,2];
    delete xs[1];
    console.log(xs); // [0, undefined, 2]
    
  • splice方法可以移除一个块或数组,移动剩余的元素以填充它。

    var xs = [0,1,2,3];
    xs.splice(2, 1); //index, ammount to remove
    console.log(xs); // [0,1,3]
    
  • 设置数组的length属性会截断它。当您需要更多控制时,这可用于以旧式方式手动删除元素。

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

所以在你的情况下我们可能会这样做:

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; });

答案 2 :(得分:1)

基本上你想要使用这样的东西:

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--;
    }
}

答案 3 :(得分:1)

这非常简单。在伪代码中:

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 ]

在JavaScript中,删除变量的命令是delete,并且可以在相同的数组上使用,例如delete someArray[ someIdx ];timeInSeconds的实现将是这样的:

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

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