1

I have a json array like this

[
  {"Id":1,
  "Name":"John"
  }, 
  {"Id":2,
  "Name":"Mathew"
  },
   {"Id":3,
  "Name":"Wilfred"
  },
   {"Id":4,
  "Name":"Gary"
  }
]

I need to implement an auto complete feature using this data. so if I search for "Wil" I should get Wilfred as result. How can I do such a search similar to the SQL LIKE in JSON array

I'm nidhin
  • 2,592
  • 6
  • 36
  • 62

1 Answers1

4

Use Array.prototype.filter

var persons = [{
  "Id": 1,
  "Name": "John"
}, {
  "Id": 2,
  "Name": "Mathew"
}, {
  "Id": 3,
  "Name": "Wilfred"
}, {
  "Id": 4,
  "Name": "Gary"
}]
var searchTerm = "Wil";
var results = persons.filter(function(person) {
  return person.Name.indexOf(searchTerm) > -1;
});
console.log(results);
Rayon
  • 36,219
  • 4
  • 49
  • 76
Arun Ghosh
  • 7,634
  • 1
  • 26
  • 38