0

I want to create keyup search in Array which contains Names using JavaScript. My arrray is like,

[
 {
  "first_name": "abc",
  "account_id": 1
 },
 {
  "first_name": "Test",
  "account_id": 2
 },
 {
  "first_name": "Hasad",
  "account_id": 3
 }
]

When user add text to text box like,

User input: a

Output: abc, Hasad

This should be work like search using sql query in database. Please help me with this.

Community
  • 1
  • 1
Kunal Dholiya
  • 335
  • 2
  • 6
  • 19

4 Answers4

1

You could .filter() your arary to only include results where first_name includes the search letter, and then .map() your array:

const arr = [
 {
  "first_name": "abc",
  "account_id": 1
 },
 {
  "first_name": "Test",
  "account_id": 2
 },
 {
  "first_name": "Hasad",
  "account_id": 3
 }
];

const search = 'a';
const res = arr.filter(({first_name}) => first_name.includes(search)).map(({first_name}) => first_name);

console.log(res);

However, this approach isn't very efficient as you need to loop each object, and then every first_name for each search. If you're looking for something efficient, you could use a prefix-trie to help you with this.

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
1

You could use a simple filter function

const data = [
 {
  "first_name": "abc",
  "account_id": 1
 },
 {
  "first_name": "Test",
  "account_id": 2
 },
 {
  "first_name": "Hasad",
  "account_id": 3
 }
];

function search(input){
  const filtered = data.filter(element => {
     for (const value of Object.values(element)) {
       if (value.toString().toLowerCase().includes(input.value.toLowerCase())) return true;
     }
  })
  document.getElementById("result").innerText=JSON.stringify(filtered)
}
<input onkeyup="search(this)">
<div id="result">[{"first_name":"abc","account_id":1},{"first_name":"Test","account_id":2},{"first_name":"Hasad","account_id":3}]</div>

please take a note of the fact that my function searched all fields of the objects, for the required value and returns list of objects meeting the criteria for you to use in any way required.

Also if the amount of data you are searching is significantly larger you may consider using debounce function to trigger it only ever so often (ex. after user stops typing for a second)

Krzysztof Krzeszewski
  • 5,912
  • 2
  • 17
  • 30
1

You could filter an map by taking a lower case strings.

var data = [{ first_name: "abc", account_id: 1 }, { first_name: "Test", account_id: 2 }, { first_name: "Hasad", account_id: 3 }],
    key = 'first_name',
    value = 'a',
    result = data
      .filter(o => o[key].toLowerCase().includes(value.toLowerCase()))
      .map(o => o[key]);

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

function searchFunction() {
  const arrayValue = [{
      "first_name": "abc",
      "account_id": 1
    },
    {
      "first_name": "Test",
      "account_id": 2
    },
    {
      "first_name": "Hasad",
      "account_id": 3
    }
  ];

  const searchValue = document.getElementById('txtsearch').value;
  const response = arrayValue.filter(({
    first_name
  }) => first_name.includes(searchValue)).map(({
    first_name
  }) => first_name);

  console.log(response);
  document.getElementById('showResponse').innerHTML = response
}
<input type='text' id='txtsearch' name='txtsearch' onkeyup="searchFunction()" />
<div id='showResponse'></div>
Parth Raval
  • 4,097
  • 3
  • 23
  • 36