/    Sign up×
Community /Pin to ProfileBookmark

Array Item Check

My Challenge…

When the user enters the page I initialize two arrays,

invArray{num:”none” name:”none” qty:”none”};
nameArray{num:”1″ name:”couch”, num:”2″ name:”sofa”}; (and so on for 100 items)

Then everytime a user completes three text boxes, I grab the info and feed it to the invArray.

txtA is an itemNumber
txtB is an itemName
txtC is an itemQty

after a user leaves txtA for txtB I search nameArray and prefill the txtB with the appropriate name.

At the same time I want to search invArray to see if that item number exists already. If it does, I want to grab it and populate the txtC with the qty. Problem is that I cannot figure out how to get around an item not existing in invArray.

Thoughts?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliApr 09.2004 — ?

typically when you try to access an element thats not initialised in an array you will get an "undefined" error....so my point is please explain the question abit more..
Copy linkTweet thisAlerts:
@ianjkingauthorApr 09.2004 — Well,

Instead of looking directly at an itemNum that may or may not exist in the array...

i.e. itemQty = invArray[itemNum].qty

I decided to use a for loop and if statements to first determine if it exists.

for(var x = 0; x < invArray.length; ++x){

if(itemNum == invArray[x].num){

itemQty = invArray[x].qty

} else {

itemQty = "";

}

}

This got the code working, not sure if there was a simpler or more direct/efficient way to write it or not to get the desired results.

Thanks!
×

Success!

Help @ianjking 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 5.25,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...