basic loop

How to create a loop that goes through all object created with functio

Page 1 of 1

12 Replies - 1189 Views - Last Post: 14 October 2010 - 03:51 PM

#1 Al101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-February 09

basic loop

Posted 14 October 2010 - 02:00 PM

Hi.
I try to use some of the OOP.

     function Tag(name, value, x, y){
            this.name    = name;
			this.value   = value;
			this.x       = x;
			this.y       = y; }

        var star = new Tag( 'sun', 29, 10, 10);
        var cube = new Tag('cube', 101, 10, 20);
        var circle = new Tag('red ball', 50, 10, 20);


now how would i create a loop that will scan all the objects that created and do sotmthing like that:

for (obj in object.Tag)
{
 alert (obj.name);
}


thank you

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 14 October 2010 - 03:31 PM


Is This A Good Question/Topic? 0
  • +

Replies To: basic loop

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,322
  • Joined: 08-June 10

Re: basic loop

Posted 14 October 2010 - 02:03 PM

you can’t with that setup, because you created 3 independent and to each other unrelated variables. it were different, if you’d used an array.
Was This Post Helpful? 0
  • +
  • -

#3 Al101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-February 09

Re: basic loop

Posted 14 October 2010 - 02:14 PM

Hi Dormilich
thanks for reply

How would i create new object using the same method (function constractor) and create an array of objects.
actualy i like that idea if i would able to create that.

Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,322
  • Joined: 08-June 10

Re: basic loop

Posted 14 October 2010 - 02:16 PM

you’d do it like every array?
var x = [new Tag(…), new Tag(…)];

Was This Post Helpful? 0
  • +
  • -

#5 Somyagupta  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 85
  • Joined: 05-August 10

Re: basic loop

Posted 14 October 2010 - 02:17 PM

Try http://www.w3schools.../js/default.asp site. It contains tutorial that tells how to create new object and array of object
Was This Post Helpful? 0
  • +
  • -

#6 Al101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-February 09

Re: basic loop

Posted 14 October 2010 - 02:36 PM

Somyagupta i looked at that link, none of the samples there addresses my problem.
I started my project with associative array.
While each object in array have about 10 properies or values.
The problem that i had - i could use for(obj in Tags)which is good but i cant do Tags.length or add new items to this array on run time.
That why i tryied this aproach. This allows me to add new items without any problems at run time.

This post has been edited by Al101: 14 October 2010 - 02:38 PM

Was This Post Helpful? 0
  • +
  • -

#7 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,322
  • Joined: 08-June 10

Re: basic loop

Posted 14 October 2010 - 02:38 PM

View PostAl101, on 14 October 2010 - 09:36 PM, said:

I started my project with associative array.

then I have bad news for you - there are no associative arrays in Javascript. What you call "associative array" is just an … object (and it naturally doesn’t have a length property)

View PostAl101, on 14 October 2010 - 09:36 PM, said:

The problem that i had - i could use for(obj in Tags)which is good but i cant do Tags.length or add new items to this array on run time.

as I already said, a general object doesn’t have a .length property. and you can always add something to an object on runtime (with very few exceptions)

This post has been edited by Dormilich: 14 October 2010 - 02:41 PM

Was This Post Helpful? 0
  • +
  • -

#8 Al101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-February 09

Re: basic loop

Posted 14 October 2010 - 02:47 PM

I would appriciate if you would give me a sample of code that does that.
Thank You.
Was This Post Helpful? 0
  • +
  • -

#9 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,322
  • Joined: 08-June 10

Re: basic loop

Posted 14 October 2010 - 02:48 PM

that does what?
Was This Post Helpful? 0
  • +
  • -

#10 Al101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-February 09

Re: basic loop

Posted 14 October 2010 - 03:27 PM

Sorry for not beeng clear,

I did try that
 function Tag(discrp, name, value, x, y){
	this.discrp = discrp;
        this.name = name;
	this.value = value;
	this.x = x;
	this.y = y; }

var TagsList = [new Tag( 'star', 'sun', 29, 10, 10), new Tag('FirstCube', 'cube', 101, 10, 20), new Tag('circle','red ball', 50, 10, 20)];

// and to add an item      
TagsList.push(new Tag( 'star2', 'zulu', 122, 10, 10));



yes that works.
since my initial array would have about 30 items, and every item has 10 values, is there a way to create the TagsList array not as a very very long list but with some kind of loop?

MOD EDIT: When posting code...USE CODE TAGS!!!

This post has been edited by JackOfAllTrades: 14 October 2010 - 03:32 PM

Was This Post Helpful? 0
  • +
  • -

#11 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,322
  • Joined: 08-June 10

Re: basic loop

Posted 14 October 2010 - 03:33 PM

that depends on the format of the values. if they’re in an array, you can do
var a = ["star", "circle"],
    b = ["sun", "ball"],
    c = [];
for (var i = 0; i--;)/> {
    c.push(new Tag(a[i], b[i]));
}

but that’s not more efficient than defining it manually.
Was This Post Helpful? 0
  • +
  • -

#12 Al101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 06-February 09

Re: basic loop

Posted 14 October 2010 - 03:45 PM

Thanks, that is quite simple.

Quote

but that’s not more efficient than defining it manually.


In which way?
Was This Post Helpful? 0
  • +
  • -

#13 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,322
  • Joined: 08-June 10

Re: basic loop

Posted 14 October 2010 - 03:51 PM

time and amount of code.

the core problem is the data for the constructor. no matter how, but that’s the least amount you have to write.

This post has been edited by Dormilich: 14 October 2010 - 03:53 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1