结构 { type: 1, lists: [ { name: 'asdf’, admin: 'asdf’, hasNum: 1, key: undefined }, { name: 'sdfsdf’, admin: 'sdfsdf’, hasNum: 1, key: undefined }, { name: '呵呵呵’, admin: '菲菲’, hasNum: 1, key: undefined }, { name: '分丰富’, admin: '首都发生地’, hasNum: 1, key: undefined } ] }
想根据 admin 的值 去删除对应的 lists 数组里的 对象
举例 我获取到 菲菲用户后 ,删除 admin 是菲菲的 对象
Array.prototype.remove = function(index){ delete this[index]; return this; }; Array.prototype.clean = function(){ var arr1 = this, arr2 = []; for(var a in arr1){ if(arr1[a]&&arr1.hasOwnProperty(a)){ arr2.push(arr1[a]); } } this.splice(0); for(var b in arr2){ if(arr2.hasOwnProperty(b)){ this.push(arr2[b]); } } return this; }; var array = [1,2,3,4]; array.remove(2).clean();
var room = {lists:[{admin:111},{admin:222}]}
function del(admin){
var pass = false;
room.lists = room.lists.filter(function(v){
return pass || (pass = !(v.admin === admin));
});
}
del(111)
console.log(room.lists); // [{admin:222}]
签名: 交流群244728015 《Node.js 服务器框架开发实战》 http://url.cn/Pn07N3
product env code :
var room = {
lists:[{admin:111},{admin:222}],
del:function(admin){
for(var i=0,len=this.lists.length;i<len;i++){
if(this.lists[i].admin === admin){
this.lists.splice(i,1);
break;
}
}
}
}
room.del(111);
console.log(room.lists); // [{admin:222}]
签名: 交流群244728015 《Node.js 服务器框架开发实战》 http://url.cn/Pn07N3