javascript - Splicing a specified user -


i'm trying remove specific user list, i'm trying make when user has id gets spliced list, reason following code doesn't splice user other user, why this?

this following test code used:

var users = [];    users.push({  	user: 1,    name: 'user1'  });    users.push({  	user: 2,    name: 'user2'  });    var interval = setinterval(function(){  	console.log(users);  }, 1000);    settimeout(function(){  	users.splice(finduser(2), 1);  }, 5000);    function finduser(id){  	for(var i=0; < users.length; i++){  		if(users[i].user === id){  			return users[i];  		}  	}  }

here fiddle.

first add 2 people list, have test interval running spitting out every user in list, after 5 seconds remove user id 2, reason doesn't work.

splice takes index remove, not value.

you can modify finduser function finduserindex function:

settimeout(function(){     users.splice(finduserindex(2), 1); }, 5000);  function finduserindex(id){     for(var i=0; < users.length; i++){         if(users[i].user === id){             return i;         }     } } 

var users = [];    users.push({  	user: 1,    name: 'user1'  });    users.push({  	user: 2,    name: 'user2'  });    var interval = setinterval(function(){  	console.log(users);  }, 1000);    settimeout(function(){  	users.splice(finduserindex(2), 1);  }, 5000);    function finduserindex(id){  	for(var i=0; < users.length; i++){  		if(users[i].user === id){  			return i;  		}  	}  }


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -