javascript - Is there a faster way to find elements in a 2D sorted array -
i wanted ask if there faster way find elements in 2d sorted array. 2d array sorted alphabetically. array contains around 900 elements. here example of data array contains:
[["alphabit","abc"],["abncoin","abn"],["crown","crw'],["cyder","cyder"],["agrello","delta"]] like can see have sorted alphabetically on second value.
what want users can type in searchbar @ top , start searching in array after values match. matches showed person.
what mean matches. if typed letter in searchbar, show results:
- alphabit, abc
- abncoin, abn
- agrello, delta
if typed in letter t in searchbar, show results:
- agrello, delta
- alphabit, abc
so mean if 1 of 2 values have letter in show it. html data in html. , have matching values in array:
this jquery code have:
var searchaftercoin = function () { var data = $('#searchbar').val().tolowercase(); if (data === '' || data === ' ') { showeverthing(searcharrayofnames.length); } else { hideeverthing(searcharrayofnames.length); (var = 0; < searcharrayofnames.length; i++) { var longname = searcharrayofnames[i][0].tolowercase(); var shortname = searcharrayofnames[i][1].tolowercase(); if (longname.indexof(data) >= 0 && shortname.indexof(data) >= 0) { $("#" + + "").show(); } else { if (longname.indexof(data) >= 0) { $("#" + + "").show(); } else { if (shortname.indexof(data) >= 0) { $("#" + + "").show(); } } } } } }; this example of html code have:
<div class="row" id="13"> // data in here// </div> like can see index of matching value in array matches id in html.
is there away use faster algorithm find elements? method hiding , showing elements not fast enough or better create html when starts search , append html then?
i'm not sure better algorithm, can make code shorter , simpler.
var matches = searcharrayofnames.reduce(function(p,c,i){ if(c[0].tolowercase().indexof(data)>-1 || c[1].tolowercase().indexof(data)>-1) p.push(i.tostring()) return p; },[]); $('div.row').filter(function(){ return matches.indexof(this.id)>-1; }).show(); the concept here use reduce find indexes of elements matching input , jquery filter find elements based on matches array.
var searcharrayofnames = [["alphabit","abc"],["abncoin","abn"],["crown","crw"],["cyder","cyder"],["agrello","delta"]]; var searchaftercoin = function () { var data = $('#searchbar').val().tolowercase(); if (data === '' || data === ' ') { showeverthing(searcharrayofnames.length); } else { hideeverthing(searcharrayofnames.length); var matches = searcharrayofnames.reduce(function(p,c,i){ if(c[0].tolowercase().indexof(data)>-1 || c[1].tolowercase().indexof(data)>-1) p.push(i.tostring()) return p; },[]); $('div.row').filter(function(){ return matches.indexof(this.id)>-1; }).show(); } }; $('#searchbar').on('keyup',searchaftercoin) function showeverthing(){ $('div').show(); } function hideeverthing(){ $('div').hide(); } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="searchbar" type="text" /> <div class="row" id="0"> alphabit data </div> <div class="row" id="1"> abncoin data </div> <div class="row" id="2"> crown data </div> <div class="row" id="3"> cyder data </div> <div class="row" id="4"> agrello data </div>
Comments
Post a Comment