I have the following array :
var Array = [{id:100,name:'N1',state:'delhi',country:'india',status:'active'},
{id:101,name:'N2',state:'kenya',country:'africa',status:'suspended'}
{id:102,name:'N3',state:'kerala',country:'india',status:'inactive'}
{id:103,name:'N4',state:'victoria',country:'australia',status:'active'}]
and I have a search field , where I need to filter the array with that searched value and return the matched object. The problem here for me is I donot know what key and value pairs may come in the above array , Key value pairs are generated dynamically ,also how can I search the array using Regex . It should match with each char I type and return the matching object in an array ? the result should look something like this :
search key : ind
[{id:100,name:'N1',state:'delhi',country:'india',status:'active'},
{id:102,name:'N3',state:'kerala',country:'india',status:'inactive'}]
search key : N2
[{id:101,name:'N2',state:'kenya',country:'africa',status:'suspended'}]
Any suggestions would be appreciated. Thanks
You could filter the array and check the value by a direct check, if you need to seach for parts of strings or for case independent values.
function search(value) { return array.filter(o => Object.values(o).some(v => v === value)); } var array = [{ id: 100, name: 'N1', state: 'delhi', country: 'india', status: 'active' }, { id: 101, name: 'N2', state: 'kenya', country: 'africa', status: 'suspended' }, { id: 102, name: 'N3', state: 'kerala', country: 'india', status: 'inactive' }, { id: 103, name: 'N4', state: 'victoria', country: 'australia', status: 'active' }]; console.log(search('india')); console.log(search('N2'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.