Skip to content

Instantly share code, notes, and snippets.

@aderaaij
Created November 14, 2017 23:39
Show Gist options
  • Save aderaaij/6059b9d2221d5fe90a647bd7a94e8589 to your computer and use it in GitHub Desktop.
Save aderaaij/6059b9d2221d5fe90a647bd7a94e8589 to your computer and use it in GitHub Desktop.

Revisions

  1. aderaaij revised this gist Nov 14, 2017. No changes.
  2. aderaaij revised this gist Nov 14, 2017. No changes.
  3. aderaaij created this gist Nov 14, 2017.
    58 changes: 58 additions & 0 deletions es6-find-findindex.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    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);