1 创建一个数组,判断数组中是否存在某个值
let a = [1, 2, 3];
a.includes(2);
var newarr = [
{ num: 1, val: 'ceshi', flag: 'aa' },
{ num: 2, val: 'ceshi2', flag: 'aa2' }
]
console.log(newarr.filter(item => item.num===2 ))
<script type="text/javascript">
var array = ["1","2","3","4"];
alert(aray.indexOf(String(2)));
alert(array.indexOf(5));
</script>
<script type="text/javascript">
var array = [1,2,3,4];
alert(array.indexOf(2));
alert(array.indexOf(5));
</script>
var arr = ['1','2',undefined, '3.jpg',undefined]
var newArr = arr.filter((element, index, array)=>{
if(element==1){
return element;
}
})
console.log(newArr)
2去掉空数组空字符串、undefined、null
var arr = ['1','2',undefined, '3.jpg',undefined]
var newArr = arr.filter(item => item)
console.log(newArr)
var arr = ['1','2',null, '3.jpg',null]
var newArr = arr.filter(item => item)
console.log(newArr)
>//空字符串里面不能包含空格
var arr = ['1','2','', '3.jpg','']
var newArr = arr.filter(item => item)
console.log(newArr)
3.去掉数组中不符合项
var arr = [20,30,50, 96,50]
var newArr = arr.filter(item => item>40)
console.log(newArr)
4.过滤不符合项
var arr = ['10','12','23','44','42']
var newArr = arr.filter(item => item.indexOf('2')<0)
console.log(newArr)
数组去重
var arr = [1, 2, 2, 3, 4, 5, 5, 6, 7, 7,8,8,0,8,6,3,4,56,2];
var arr2 = arr.filter((x, index,self)=>self.indexOf(x)===index)
console.log(arr2); //[1, 2, 3, 4, 5, 6, 7, 8, 0, 56]
网友评论