/    Sign up×
Community /Pin to ProfileBookmark

Using array with key

I have this code which now works, well it does when I use the one-dimensional list, as soon as I add the key is doesn’t work and I know why but am so confused on how to fix it.

Any help?

var targetInput = document.getElementById(“aircraft”),
results = document.getElementById(“autocomplete-results”),
//aircraftList = [‘Airbus A320-200′,’Airbus A330-200′,’Airbus A380-800′,’Boeing 737-200′,’Boeing 777-200ER’],
aircraftList = {‘Airbus A320-200’:1,
‘Airbus A330-200’:2,
‘Airbus A380-800’:3,
‘Boeing 737-200’:4,
‘Boeing 777-200ER’:5 },
matches = [];

//targetInput.focus();
//targetInput.addEventListener(“keyup”, function(event){
$(document).on(‘keyup’,’#aircraft’,function(){

$(“#autocomplete-results”).html(“”);
toggleResults(“hide”);

if (this.value.length > 0){
matches = getMatches(this.value);
if (matches.length > 0){
displayMatches(matches);
}
}

});

function toggleResults(action){
if (action == ‘show’){
$(“#autocomplete-results”).addClass(‘visible’);
} else {
$(“#autocomplete-results”).removeClass(‘visible’);
}

}

function getMatches (inputText){
var matchList = [];
for(var i = 0; i < aircraftList.length; i++){
if (aircraftList[i].toLowerCase().indexOf(inputText.toLowerCase()) != -1){
matchList.push(aircraftList[i]);
}
}
return matchList;
}

function displayMatches(matchList){
var j = 0;
while (j < matchList.length){
console.log(‘dd’+ matchList[j]);
$(“#autocomplete-results”).append(‘<li class=”result”>’ + matchList[j] + ‘</li>’);
j++;
}

toggleResults(“show”);

}

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@rootDec 09.2018 — same as you use arrayname[ indices ] you arrayname[ propertyname ] where these elements are not traversed in looped arrays, so anythijg you do with them will need to use the for..in looping operation
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 3.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,
)...