1.some方法数组中满足提供的测试函数的第一个元素的true.否则返回 false
var arr = [{
name: "小提莫",
value: "0"
}, {
name: "小鲁班",
value: "1"
}]
var arr1 = arr.some(function(item) {
if (item.name == "小提莫") { //item.name == "小百里守约"
return true; //返回false
}
})
console.log(arr1);
2.find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回 [undefined
]
var arr = [{
name: "小提莫",
value: "0"
}, {
name: "小鲁班",
value: "1"
}]
var arr1= arr.find((v) => {
return v.value === '0'; // return v.value === '9' 返回undefined
});
console.log(arr1);
3.findIndex()方法返回数组中满足提供的测试函数的第一个元素的索引。否则返回-1
var arr= [{
name: "小提莫",
value: "0"
}, {
name: "小鲁班",
value: "1"
}]
var arr1= arr.findIndex((v) => {
return v.value === '0';//return v.value === '9' 返回-1
});
console.log(arr1);
4.键值对(去重)
var arr= [{
name: "小提莫",
value: "0"
}, {
name: "小鲁班",
value: "1"
}, {
name: "小龙虾",
value: "2"
}, {
name: "小龙虾",
value: "2"
}]
function unique(arr){
let obj = {};
return arr.filter((item, index, array) =>{
return obj.hasOwnProperty(typeof item.value + JSON.stringify(item.value)) ?
false : (obj[typeof item.value + JSON.stringify(item.value)] = true)
})
}
console.log(unique(arr));
/*
var arr= [{
name: "小提莫",
value: "0"
}, {
name: "小鲁班",
value: "1"
}, {
name: "小龙虾",
value: "2"
}]
*/
侵删