/    Sign up×
Community /Pin to ProfileBookmark

Looping through an array

I have an array that is going to contain about 400 things. Now I need to search for a specific item in the array and I was wondering if there was a more efficient way than a “for” loop and a “if” conditional in this for when the array matches the item I’m searching for. npicID is an ID that I get from the URL. Here is what I have:

var getPictureName = new array(400)

getPictureName(1) = “a1/smiley.gif”;
etc.

for (i = 0; i <= 400; i++) {
if (getPictureName(i) == npicID) {

var image= getPictureName(i);
var NS = (navigator.appName==”Netscape”)?true:false;

iWidth = (NS)?window.innerWidth:document.body.clientWidth;
iHeight = (NS)?window.innerHeight:document.body.clientHeight;
iWidth = document.images[0].width – iWidth + 75;
iHeight = document.images[0].height – iHeight + 210;
window.resizeBy(iWidth, iHeight);
self.focus();

}
}

The above loop will work but it just seems like it will take forever and also be a waste of resources. Anyone know of a cleaner and more efficient process. Thanks.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@CharlesJul 24.2004 — JavaScript doesn't have a built in "grep" method but you might be able to restructure your data in a more useful way. Instead of an Array you might use an Object as something like a Perl hash of boolean.

[font=monospace]<script type="text/javascript">

<!--

giantSays = {fee:1, fie:1, foe:1, fum:1}

alert (Boolean(giantSays.foo))

// -->

</script>[/font]
Copy linkTweet thisAlerts:
@Mr_JJul 24.2004 — Why not just pass the image as an argument to a function

Or pass the array index number, why have it search for something that you know is there?

<script type="text/javascript">

<!--

function test(pic){

var image= new Image()

image.src=pic

imagewidth=image.width

imageheight=image.height

var NS = (navigator.appName=="Netscape")?true:false;

iWidth = (NS)?window.innerWidth:document.body.clientWidth;

iHeight = (NS)?window.innerHeight:document.body.clientHeight;

iWidth = imagewidth + 75;

iHeight = imageheight + 210;

window.resizeBy(iWidth, iHeight);

self.focus();

}

// -->

</script>

<a href="#null" onclick="test('pic1.jpg')">Test</a>
Copy linkTweet thisAlerts:
@BobotheBugbearauthorJul 24.2004 — Its a good idea but can I do this when I'm going from 1 page to another. I guess I could put the image name in the URL and extract it and run it in the function specifically.
×

Success!

Help @BobotheBugbear 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.20,
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,
)...