2 Replies - 578 Views - Last Post: 11 October 2011 - 08:36 AM

#1 lil_bugga   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 29-October 10

dodgy array building?

Posted 09 October 2011 - 04:59 AM

Hi peeps. I'm having some issues with my code. I'm trying to pull data from one array and use it to populate a new array. With my initial array I can get the results of certain properties using 'servos[i].application'. Any servos that share their value with that of my filter box should be put into a new array called matches and allow me to referance them in a similar way 'matches[i].application'

At the minute I've got my code to populate the new matches array, i'm pretty sure, but when I try to get data out of the second array using the 'matches[i].application' style of query it says its undefined. I lready have an array called servos which is full of 49 different servo objects.

I've added the function containing this code and commented it too. Hopefully one of you can see what I'm missing.

function dataPull(){
//create servoStore to build page for display
//var servoStore="";
			
// search and get servos meeting application filter settings
	// create new array called matches with set properties
	var matches = new Array("application", "sclass", "type", "motor", "bearings", "gears", "modelno", "name", "speed", "v3_3", "v4_8", "v6_0", "v7_2", "weight", "dimensions", "opvoltage", "image", "description");
	
	// loop through servos array and pull any servo that has a matching application value to that selected by the search filter
	for(var i=0; i < servos.length; i++){
		if (servos[i].application == document.searchFilters.applicationMenu.value)  {
			// populate the new matches array with the details from the servos pulled from the inital array
			matches[c] = new servo(servos[i].application, servos[i].sclass, servos[i].type, servos[i].motor, servos[i].bearings, servos[i].gears, servos[i].modelno, servos[i].name, servos[i].speed, servos[i].v3_3, servos[i].v4_8, servos[i].v6_0, servos[i].v7_2, servos[i].weight, servos[i].dimensions,servos[i].opvoltage, servos[i].image, servos[i].description);
			c++;
		} else if (document.searchFilters.applicationMenu.value == 0) {
			// sets the value of servoStore locally
			var servoStore = 0;
		}

// if application filter is set to 'show all' loop through all servos for display inside a table
			var counter=1; // used for display purposes
			display_content = "<table border='0' width='950px' align='center'>";
			display_content += "<tr>\n";
			
			if (servoStore == 0){
			for(var i=0; i < servos.length; i++){
				display_content += "<td>\n";
				display_content += "<table border='0' width='180px' height='200'><tr><td><p>"+ servos[i].modelno +"</p></td></tr><tr><td><img src="+ servos[i].image +"></br></br> name " + servos[i].name + " speed " + servos[i].speed + " v3_3 " + servos[i].v3_3 + " v4_8 " + servos[i].v4_8 + " v6_0 " + servos[i].v6_0 + " v7_2 " + servos[i].v7_2 + " weight " + servos[i].weight + " dimensions " + servos[i].dimensions + " opvoltage " + servos[i].opvoltage + " description " + servos[i].description +"</td></tr></table><hr/>";	
				display_content += "</td>";
				counter++;
				if(counter >4) {
					display_content += "</tr>\n";
					counter=1;
				}
			}
			display_content += "</table>";
			document.getElementById('servoDisplay').innerHTML = display_content; // display all servos if search filter = 0
		} else {
			document.getElementById('servoDisplay').innerHTML = "search result " + matches[c].modelno ; // display servos model numbers stored within the matches array
		}
	}
};


Is This A Good Question/Topic? 0
  • +

Replies To: dodgy array building?

#2 lil_bugga   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 52
  • Joined: 29-October 10

Re: dodgy array building?

Posted 11 October 2011 - 03:50 AM

I'm still having issues with this so I've decided to break it all down and go bit by bit. So my question now is how do you create an array with specific properties.

I've tried so many different things to make it work and just about lost the plot, is this right/close?

myarray(property 1, property 2, property 3)
my array[i]=(value 1, value 2, value 3)


my goal is to be able to use
myarray.property 1
in IF statments and loops etc

This post has been edited by lil_bugga: 11 October 2011 - 03:51 AM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7181
  • View blog
  • Posts: 14,969
  • Joined: 16-October 07

Re: dodgy array building?

Posted 11 October 2011 - 08:36 AM

You don't seem to want an array, but rather a simple object.

e.g.
var MyProps = {
   'property1':1, 'property2':'hi', 'property3': 0
};
if (MyProps.property1==1) { //...


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1