/    Sign up×
Community /Pin to ProfileBookmark

Searching Two Dimensional Array

Can anyone tell me why this code would not work? I’ve found examples using google that are similar, but this code doesn’t do either of the branches (true or false). It just poops out. I know that there is data in the array because I can display them sequentially if I want to.

It’s using a two dimensional array and I want to display one of the values.

Code:

findValue(array, ‘Selection1’)

function findValue(arrayObj, searchFor)
{

for (i=0;i<arrayObj.length;i++)
{
var found = false
if (arrayObj[i][0] == searchFor)
{
document.write(arrayObj[i][0])
var found = true
return true
break
}
else if (((i == (arrayObj.length-1)) && !found)){
document.write(“Not found!”)
return false
}
}

}

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@hacketauthorJun 19.2009 — Well I determined I'm having a problem with something else that's affecting this function.

What I need is a way to access the individual characters of a string to compare them to another character.

Such as: if(string[char] = compareTo){

do this

}

However, this syntax doesn't work. Any thoughts?
Copy linkTweet thisAlerts:
@JMRKERJun 19.2009 — Try 'charAt()' of 'indexOf()' functions ...

Example:
<i>
</i>var str = 'abcdefghijklmnopqrstuvwxyz';
var tmp = '';
for (var i=0; i&lt;str.length; i++) { tmp += i+': '+str.charAt(i)+'n'; }
alert('charAt(i) : returnsn'+tmp);

// Try an invalid input here, like 'A' and you will see a "-1" for the position
var ch = prompt('Enter a letter (a-z):','a');
alert(str.indexOf(ch)+' is the position of: '+ch+'nin the array: '+str);
×

Success!

Help @hacket 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...