# I am tring to fetch data from api and search data. but I a little bit confusing how to do that

**URL:** <https://forum.freecodecamp.org/t/i-am-tring-to-fetch-data-from-api-and-search-data-but-i-a-little-bit-confusing-how-to-do-that/403510>\
**Category:** General\
**Created:** [June 14, 2020, 7:15pm UTC](https://forum.freecodecamp.org/t/i-am-tring-to-fetch-data-from-api-and-search-data-but-i-a-little-bit-confusing-how-to-do-that/403510 "2020-06-14T19:15:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HaithamCa](https://avatars.discourse-cdn.com/v4/letter/h/278dde/32.png) [@HaithamCa](https://forum.freecodecamp.org/u/HaithamCa)\
**Post date:** [June 14, 2020, 7:15pm UTC](https://forum.freecodecamp.org/t/i-am-tring-to-fetch-data-from-api-and-search-data-but-i-a-little-bit-confusing-how-to-do-that/403510/1 "2020-06-14T19:15:17Z")

</div>

```auto
const search = document.querySelector('#search');
const matchList = document.querySelector('#match-list');
let studentsDetails;

const searchStudents = async searchText => {
    const res = await fetch('https://www.hatchways.io/api/assessment/students');

    const fetchStudents = await res.json();
    const studentsDetails = fetchStudents.students;
    console.log(studentsDetails);
    var content = [];
    for (var i = 0; i < studentsDetails.length; i++) {
        content.push(studentsDetails[i].firstName);
    }
    console.log(content);
    // Match to current text input
    let matches = content.filter(student => {
        const regex = new RegExp(`^${searchText}`, 'gi');
        return content.firstName.match(regex) || content.lastName.match(regex);

    });
    console.log(matches);

}

search.addEventListener('input', () => searchStudents(search.value));

```

---

<div class="post-metadata">

**Author:** ![Manish-Giri](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/manish-giri/32/357053_2.png) [@Manish-Giri](https://forum.freecodecamp.org/u/Manish-Giri)\
**Post date:** [June 14, 2020, 8:55pm UTC](https://forum.freecodecamp.org/t/i-am-tring-to-fetch-data-from-api-and-search-data-but-i-a-little-bit-confusing-how-to-do-that/403510/2 "2020-06-14T20:55:42Z")

</div>

Is your question about the RegEx? Because your function seems to be working correctly, as far as getting the API response is concerned.

---

<div class="post-metadata">

**Author:** ![HaithamCa](https://avatars.discourse-cdn.com/v4/letter/h/278dde/32.png) [@HaithamCa](https://forum.freecodecamp.org/u/HaithamCa)\
**Post date:** [June 14, 2020, 9:02pm UTC](https://forum.freecodecamp.org/t/i-am-tring-to-fetch-data-from-api-and-search-data-but-i-a-little-bit-confusing-how-to-do-that/403510/3 "2020-06-14T21:02:02Z")

</div>

I want to filter a data when typing in search bar.

---

<div class="post-metadata">

**Author:** ![Manish-Giri](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/manish-giri/32/357053_2.png) [@Manish-Giri](https://forum.freecodecamp.org/u/Manish-Giri)\
**Post date:** [June 14, 2020, 9:19pm UTC](https://forum.freecodecamp.org/t/i-am-tring-to-fetch-data-from-api-and-search-data-but-i-a-little-bit-confusing-how-to-do-that/403510/4 "2020-06-14T21:19:25Z")

</div>

Your regex search is incorrect.

First, you want to use `student` to run `.match()` on, instead of `content.firstName.match`, because `student` will be the current string being iterated upon.

And next, I don’t think you’ve pushed last names onto `content`, so this won’t work anyway -  
`content.lastName.match(regex)`.

If I change your code to -

```auto
let matches = content.filter(student => {
        const regex = new RegExp(`^${searchText}`, 'gi');
        return student.match(regex);

    });
  
   console.log(matches);

```

And call the function with `searchStudents('Mureil')`, I get - `["Mureil"]`

---

<div class="post-metadata">

**Author:** ![HaithamCa](https://avatars.discourse-cdn.com/v4/letter/h/278dde/32.png) [@HaithamCa](https://forum.freecodecamp.org/u/HaithamCa)\
**Post date:** [June 14, 2020, 9:41pm UTC](https://forum.freecodecamp.org/t/i-am-tring-to-fetch-data-from-api-and-search-data-but-i-a-little-bit-confusing-how-to-do-that/403510/5 "2020-06-14T21:41:52Z")

</div>

Yes it working know but just for the first name I want it also by last name and get all data about this student. because it just give me his name.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:49pm UTC](https://forum.freecodecamp.org/t/i-am-tring-to-fetch-data-from-api-and-search-data-but-i-a-little-bit-confusing-how-to-do-that/403510/6 "2021-06-01T18:49:56Z")

</div>

Closed on June 1, 2021, 6:49pm UTC


