/    Sign up×
Community /Pin to ProfileBookmark

Comparing data in 2 arrays

What I’m attempting to do is to return a list from a database in the form of an array and compare it against a state array. What I want to do is loop through the Contact Array and if it does not find a match to the Results Set, dead link it. If it does find a result, I want it linked using <a href = “value”></a>

I know how to write the html piece of it in JS, but am stuck with how to do this comparison so that ALL of the states show, but only those which have a value from the database will have a link.

The column alignments aren’t the best below, but you get the idea.

[FONT=”Courier New”]Constant Array Results Set (from database in the form of an array)
————– ———–
AL
AK AK
AZ AZ
AR
CA CA
CO CO
CT CT
DE [/FONT]

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@Mr_MooAug 22.2007 — I am making the assumptions that you have an array called constants which are your constants and an array called results which are the database results, that they both contain strings, and that a constant can have only one result:
<i>
</i>var html = "&lt;table&gt;";
for(var i = 0; i &lt; constants.length; ++i) {
html += "&lt;tr&gt;&lt;td&gt;" + constants[i] + "&lt;/td&gt;&lt;td&gt;";
for(var j = 0; j &lt; results.length; ++j) {
if(constants[i] == results[j]) {
html += results[j];
break;
}
}
html += "&lt;/td&gt;&lt;/tr&gt;";
}
html += "&lt;/table&gt;";


The code above is not the best, but you get the idea ;-)
×

Success!

Help @daniboi81 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.6,
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,
)...