inherit
217348
0
Jul 27, 2022 7:26:44 GMT -8
Lynx
5,849
January 2015
msg
|
Post by Lynx on Nov 29, 2015 22:33:21 GMT -8
I'm back! Okay, if I'm understanding correctly, if you push something, it gets appended to what's already in there (if anything) and when you pop out of it, the last item pushed is the first one that's popped. Now, if I'm correct in that, is there an easy way to clear out the holder or does everything have to be popped out one at a time. Allow me to use a line from my marquee plugin: this.mqo = document.getElementById(id); (so you can see what this.mqo is) mqr.push(this.mqo);If I'm reading this right, whatever this.mqo is equal to at the time of the push, it gets appended to whatever is already in mqr, or becomes the first item in "mqr" if nothing in there. If 6 items were pushed into "mqr", is there an easy way to clear "mqr" or do I have to do a for loop to pop each item? Also, if 6 items were pushed into "mqr", does this basically make "mqr" a very long list or are they stored as an array that can be accessed via: mqr[0], mqr[1], etc.? Thanks!
|
|
inherit
2671
0
May 14, 2013 14:40:03 GMT -8
Peter
🐺
10,615
February 2002
peter3
|
Post by Peter on Nov 30, 2015 1:39:07 GMT -8
You are correct with the push and pop.
Easiest way to empty an array, is to set it as a new array or setting its length to 0.
In your case though, I would add some fallback if the element with that ID you are after doesn't exist, because you can't push to a "null" value.
Examples:
In your code, if the element doesn't exist, then when you push to it, it will error.
this.mqo = document.getElementById(id);
mqr.push(this.mqo);
An easy way to prevent that, is to either check that "mqo" does exist (i.e is not null and maybe has a length), or set it as an array as a fallback like so...
this.mqo = document.getElementById(id) || [];
mqr.push(this.mqo);
Emptying an array is as easy as setting it as a new array again.
this.mqo = document.getElementById(id) || [];
mqr.push(this.mqo);
mgr = [];
There are a few different ways to empty an array, but that way is the most common.
You can access each element in the array by its index value.
mgr[0] // 1st mgr[4] // 5th
|
|
inherit
217348
0
Jul 27, 2022 7:26:44 GMT -8
Lynx
5,849
January 2015
msg
|
Post by Lynx on Nov 30, 2015 7:22:39 GMT -8
Thanks, Peter! Much appreciated!
|
|
Former Member
inherit
guest@proboards.com
225992
0
Nov 30, 2024 13:47:28 GMT -8
Former Member
0
January 1970
Former Member
|
Post by Former Member on Nov 30, 2015 17:35:27 GMT -8
To build on what Peter said. You can declare an array as a property of an object. Most people create an Init function that they use as a constructor of sorts. In the constructor you only want to initialize variables/properties. If you want an array NOT to mutate(only hold x values), you can check it's length. then remove the first item using the shift() method prototype, then push(). Not quite like a queue or stack that is implemented in other languages. var myobject = { arr : [], init : function(){ this.arr = [0,1,2,3,4]; }, test : function(){ if(this.arr.length === 5){ this.arr.shift(); // removes 0 }
this.arr.push(5); // [1,2,3,4,5]; }, } Your naming convention must be more verbose "mqo" and "mqr" have NO meaning to other developers trying to help!
|
|