数组中是否存在指定值,存在就删除
var str = ["a", "b", "c"];
var index = str.indexOf("a");
if(index>-1){//大于0 代表存在,
str.splice(index,1);//存在就删除
}
console.log(str);// ["b", "c"]
数组对象中是否存在指定值(方法一),存在即删除
var searchinfo =[
{ key: '999', name: 'zhangsan'},
{ key: '111', name: 'lisi'},
{ key: '222', name: 'wanger'},
{ key: '333', name: 'apple'},
{ key: '444', name: 'orange'},
]
for (var i = 0; i < searchinfo.length; i++) {
if ((searchinfo[i].key).indexOf("999") > -1) {//判断key为999的对象是否存在,
index = i;
searchinfo.splice(index, 1);//存在即删除
}
}
console.log(searchinfo);