javascript ES6 - Array.find和Array.findIndex

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript ES6 - Array.find和Array.findIndex相关的知识,希望对你有一定的参考价值。

  const users = [
    {
        "_id": "5a0b7a6441a687091332f11b",
        "index": 0,
        "guid": "0d3bba05-ecde-4237-b53e-0bd474239ee2",
        "isActive": false,
        "balance": "$2,144.22",
        "picture": "http://placehold.it/32x32",
        "age": 32,
        "eyeColor": "green",
        "name": "Lavonne Finley",
        "gender": "female"
    },
    {
        "_id": "5a0b7a647febf037be5daaa7",
        "index": 1,
        "guid": "20311487-7e92-48ba-a26f-d627d7cb3686",
        "isActive": false,
        "balance": "$2,284.92",
        "picture": "http://placehold.it/32x32",
        "age": 21,
        "eyeColor": "blue",
        "name": "Austin Mullins",
        "gender": "male"
    },
    {
        "_id": "5a0b7a64ed89bcc74e06d289",
        "index": 2,
        "guid": "211f93e7-73b9-46a5-80db-bc04908af917",
        "isActive": false,
        "balance": "$1,581.91",
        "picture": "http://placehold.it/32x32",
        "age": 32,
        "eyeColor": "brown",
        "name": "Henderson Bryant",
        "gender": "male"
    },
    {
        "_id": "5a0b7a64fdb857c6a2b5767a",
        "index": 3,
        "guid": "2e1e89de-06c2-45dd-b92e-afb4a9702026",
        "isActive": true,
        "balance": "$3,069.90",
        "picture": "http://placehold.it/32x32",
        "age": 35,
        "eyeColor": "blue",
        "name": "Mia House",
        "gender": "female"
    }
]

// Return the user with the matching id
const user = users.find(user => user._id === '5a0b7a64ed89bcc74e06d289');
console.log(user);

// Return the index of the user with the matching ID
const userIndex = users.findIndex(user => user._id === '5a0b7a64ed89bcc74e06d289');
console.log(userIndex);

以上是关于javascript ES6 - Array.find和Array.findIndex的主要内容,如果未能解决你的问题,请参考以下文章

ES6---JavaScript

javascript ES6 Set和Spread Array Unique

JavaScript ES6 - 数组扩展

Javascript / ES6 forEach Issue

JavaScript------解构赋值

Javascript学习笔记