es6 find方法用于查找第一个符合条件的数组成员,如果没有找到返回undefind

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>find方法</title>
</head>
<body>

</body>
<script>
    let arr = [
        {
            id:1,
            name:'zhangsan'
        },
        {
            id:2,
            name:'lisi'
        }
    ];

    //find方法用于查找第一个符合条件的数组成员,如果没有找到返回undefind
    let target = arr.find((item,index) => {
        return item.id ==1
    })
    console.log(target)
</script>
</html>

运行结果

原文地址:https://www.cnblogs.com/malong1992/p/12774115.html