Solved

Need help with javascript array

Posted on 2009-04-13
3
179 Views
Last Modified: 2012-05-06
Hi all.  Thank you in advance.

First I create an array like this:

var myArray = [];

I have some objects that are keeping track of information for me.  I have created them as thus:

var x = {a: "someinfo", b: "some more info" myID: "ID0"}

Then I add it to an Array like this:

myArray.push(x);

I set x to null, and will use it again in the future to possibly add more objects to the array. All of my objects have the exact same structure, but of course have a different "myID"

This "seems" to work fine for me except when it comes time to pull the information for one of the objects out of the array. I try to get the index as follows:

var myX = searchArray(myArray, "ID0");

and my searchArray function looks like this:

searchArray : function(arr, toFind)
{
  if(arr.constructor != Array)
    {
    return -1;
    }
    for(var i = 0, len = arr.length; i < len; i++)
    {
      if(arr[i].myID == toFind)
      {
        return i;
      }
    }
return -2;
}

I thought this would loop through the array until it found a matching myID and then send that index back to me so I could use the index to pull the object from the array.  When I have multiple objects in the array, and I write out whet the myID "should" be, it always comes back as 'undefined'.  I don't understand this at all.  There are clearly as many objects in the array as I have added, and on another function, it will actually loop through and write the myID.

I am so confused, and not very good with javascript (obviously).

This shouldn't be this difficult.  Can anyone help?

Thank you.

Steve
0
Comment
Question by:sstolp2005
  • 2
3 Comments
 
LVL 41

Accepted Solution

by:
HonorGod earned 500 total points
ID: 24132007
Correcting  the syntax...  this works for me...

The external script file (objDisplay.js) contains some code that I wrote to help me dump the contents of objects.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Template</title>
<script type='text/javascript' src='../objDisplay.js'></script>
<script type='text/javascript'>
  function searchArray(arr, toFind) {
    if ( arr.constructor != Array ) {
      alert( 'not array' )
      return -1;
    }
    var len = arr.length
    for( var i = 0; i < len; i++ ) {
      if( arr[ i ].myID == toFind ) {
        alert( 'found at: ' + i )
        return i;
      }
    }
    alert( 'not found' )
    return -2;
  }
 
  window.onload = function() {
    var debug = document.getElementById( 'debug' )
 
    var myArray = []
    debug.innerHTML = objDisplay( 'myArray', myArray )
    debug.innerHTML += '<hr><br>'
 
    var x = {a: "someinfo", b: "some more info", myID: "ID0"}
    debug.innerHTML += objDisplay( '<br>x', x )
    debug.innerHTML += '<hr><br>'
 
    myArray.push( x )
 
    debug.innerHTML += objDisplay( '<br>myArray', myArray )
    debug.innerHTML += '<hr><br>'
 
    debug.innerHTML += objDisplay( '<br>myArray[0]', myArray[0] )
    debug.innerHTML += '<hr><br>'
 
    x = null
 
    debug.innerHTML += objDisplay( '<br>myArray[0]', myArray[0] )
 
    var inx = searchArray( myArray, "ID0" )
  }
</script>
</head>
<body>
<div id='debug'></div>
</body>
</html>

Open in new window

objDisplay.js.txt
0
 

Author Comment

by:sstolp2005
ID: 24132324
Wow.  This really helped.  I think I can figure it out from here.  Thank you SO much!!
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24132555
You are very welcome.  Thank you for the grade & points.

Good luck & have a great day.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Capture logon name 13 73
HTML5 Get data in table rows 5 28
Select case on click 3 15
Button and js nou working 3 7
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question