user522372
user522372

Reputation: 13

array of objects containing arrays possible?

I am trying to get a slideshow to run on my page which draws from several groups (categories) of images which are included in the slideshow if a particular checkbox (one for each category) on the page is checked. I successfully coded this with two categories, but am trying to make the code more easily customizable. I had the brilliant idea to use custom objects instead of arrays. I figure this approach would probably also get me closer to my final goal of using an xml document in the process.

I am fairly new to object oriented programming, especially with javascript. I have successfully created a declaration of objects for use in a slideshow page with the following code:

function picsobj(description,length,indivpicarray){
  this.description=description;
  this.length=length;
  this.indivpicarray=indivpicarray;
}

and elsewhere the following code to make an array of picsobj objects

for(i=0;i<2;i++){ //change i< integer to number of picture arrays
  picarrays[i]=new picsobj();}

the plan is to use the description property for captions or to describe elements on the page, to use the length property to help determine how many pictures to cycle through, and to use (here's where my question comes...) the object property called indivpicarray to store an ARRAY of image names (the length of the array would change from picsobj to picsobj). I do not know if this is possible, and if it is I need help with the syntax please. Thank you for reading my question. again, sorry if there are any misused words i'm a bit of a n00b and have pretty much learned through "view source, copy, paste, alter"

Upvotes: 1

Views: 2563

Answers (5)

Emilio
Emilio

Reputation: 108

/* The function constructor of your objects usually use a capitalized names 
 * */
var Picsobj = function(description,indivpicarray){
  this.description=description;
  this.indivpicarray=indivpicarray;
};
/* Is better convert length to a method instead a property passed to the constructor. */ 
/* The methods are attached to the prototype propertie of the constructor. `this` refers to 
 * the actual object where the method is called.
 *  */ 
Picsobj.prototype.getLength = function() {
  if(typeof(this.indivpicarray) != 'object')
    return 0;
  return this.indivpicarray.length;
};


/* Example */
var pic = new Picsobj('Description of pic', ['pic1.jpg','pic2.jpg']);
var anotherPic = new Picsobj('Description of anotherPic', ['anotherPic1.jpg','anotherPic2.jpg']);
console.log(pic.getLength());
console.log(anotherPic.getLength());

Upvotes: 0

Gordon Gustafson
Gordon Gustafson

Reputation: 41239

and to use the object property called indivpicarray to store an ARRAY of image names.

This is perfectly fine. The technique of objects having properties that are arrays is quite common. Arrays are treated just like any other variable. Just add this to your constructor:

function picsobj(description,length,indivpicarray){
  this.description=description;
  this.length=length;
  this.indivpicarray=indivpicarray;
}

imageNameArray = [ "image1.png", "image2.gif", "image3.jpg" ];
var myPicsObj = new picsobj( "this is the description", 3, imageNameArray );

see here if you're not sure what a constructor is

Upvotes: 3

Lukas Eder
Lukas Eder

Reputation: 221105

There is an object called Array in JavaScript, which should do exactly what you need. See examples on http://www.w3schools.com/JS/js_obj_array.asp, or just google "javascript Array"

Hope that helps

Upvotes: 0

thejh
thejh

Reputation: 45578

Yes, that's possible. You could, for example, do it this way if you already know the values when programming it:

var picarrays=[{description:"bla","length":1234,"indivpicarray":["a","b","c"]},
               {description:"blah","length":145,"indivpicarray":["a","c","g"]}];

This gives you an array of objects of arrays.

Upvotes: 0

methodin
methodin

Reputation: 6710

In javascript anything is possible:

this.description = ["a","b","c"];

You'd have an array of 3 items being strings. In the real world you'd define it as this.description = []; then do something like:

picarrays[i].description.push("a");
picarrays[i].description.push("b");
picarrays[i].description.push("c");

Giving you effectively the same array and you can loop through .description to get all the strings in the array. You also might not need length since description.length would give you the number of items in description.

Upvotes: 0

Related Questions