javascript - How to delete an item from array of objects? -


    var arr = [                      {id:2,date:'2010-10-03',des:'goodday'},                      {id:3,date:'2011-02-13',des:'badday'},                      {id:4,date:'2011-04-03',des:'niceday'}                    ]; 

now want delete {id:3,date:'2011-02-13',des:'badday'} , , arr should be

        var arr = [                      {id:2,date:'2010-10-03',des:'goodday'},                      {id:4,date:'2011-04-03',des:'niceday'}                    ]; 

how should do?

assume id fields in objects unique can following delete it. function use splice:

    var arr = [         {         id: 2,         date: '2010-10-03',         des: 'goodday'},     {         id: 3,         date: '2011-02-13',         des: 'badday'},     {         id: 4,         date: '2011-04-03',         des: 'niceday'}     ];      for(var i=0; i<arr.length; i++){         if(arr[i].id == 3){             arr.splice(i, 1);  //removes 1 element @ position              break;         }     }  console.log(arr);   //should give  //                      var arr = [ //                               {id:2,date:'2010-10-03',des:'goodday'}, //                               {id:4,date:'2011-04-03',des:'niceday'} //                          ]; 

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -