15-3、find()和findIndex()

92 阅读1分钟
<script>
    // find(): 找到满足条件的一个立即返回
    // findIndex(): 找到满足条件的一个,立即返回其索引

    // 1.基本用法
    // console.log([1, 5, 10, 15].find((value, index, arr) => {
    //     // console.log(value, index, arr);     
    //     return value > 9
    // }));

    // console.log([1, 5, 10, 15].findIndex((value, index, arr) => {
    //     // console.log(value, index, arr);     
    //     return value > 9
    // }));

    // 2.应用
    const students = [
        {
            name: '张三',
            sex: '男',
            age: 16
        },
        {
            name: '李四',
            sex: '女',
            age: 22
        },
        {
            name: '王二麻子',
            sex: '男',
            age: 32
        },
    ];
    console.log(students.find(value => value.sex === '女'))
    console.log(students.findIndex(value => value.sex === '女'))
</script>