SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot rhlowe's Avatar
    Join Date
    Sep 2006
    Location
    San Diego
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Convert Object to Array???

    How do I explain this? I am grabbing several elements from my document and placing them in an array, using jQuery. When I test the
    Code:
    typeof
    this item, it returns as an Object, not an Array, which is problematic for IE. Firefox seems not to have an issue with
    Code:
    Object.length
    , but IE won't have any of that, so I need
    Code:
    Array.length
    or do I? In the end, I just want IE to know how many keys are in the Array/Object...
    San Diego Freelance Web Development Contractor
    Great Taste, no Filler. Now with no Trans Fat!

  2. #2
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can test to see if an object is really an Array by using instanceof:

    Code javascript:
    if ( foo instanceof Array ) {

    If you have an Object (created with { } or new Object (), you can check how many keys it contains with a function like this:

    Code javascript:
    function countProperties ( obj ) {
      var count = 0;
      for (var i in obj) {
        count++
      }
      return count;
    }

  3. #3
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are selecting your elements with jQuery, you can use length on your jQuery object.
    Code JavaScript:
    var $foos = $("p > .foo");
     
    // All jQuery objects have a length property.
    alert($foos.length);
     
    // Alternatively, you can use the size() method.
    alert($foos.size());

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, the HTMLCollection object has a built in length property, so that isn't just jQuery magic.

  5. #5
    SitePoint Zealot rhlowe's Avatar
    Join Date
    Sep 2006
    Location
    San Diego
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Here is the code I am using

    I have tried the suggestions above and none seem to get it working in IE, here is the code

    http://www.roberthenrylowe.com/lab/j...converter.html
    http://www.roberthenrylowe.com/lab/j...deconverter.js
    http://www.roberthenrylowe.com/lab/j...econverter.css

    You'll see that the code works properly in Firefox, but not in IE.
    San Diego Freelance Web Development Contractor
    Great Taste, no Filler. Now with no Trans Fat!

  6. #6
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does this help?
    Code:
    <script type="text/javascript">
    Array.prototype.isItAnArray = true; // give this property only to real arrays
    
    function getLength(thing) {
    	if (typeof thing == "object" && !thing.isItAnArray) {
    		var count = 0;
    		for (var test in thing) {
    			count++;
    		}
    		return count;
    	} else {
    		return thing.length;
    	}
    }
    
    window.onload = function () {
    	var myArray = new Array(1,2,3,4,5);
    	
    	var myObject = new Object();
    	myObject.test1 = 1;
    	myObject.test2 = 2;
    	myObject.test3 = 3;
    	myObject.test4 = 4;
    	myObject.test5 = 5;
    	
    	alert(getLength(myArray));
    	alert(getLength(myObject));
    }
    	
    </script>

  7. #7
    SitePoint Zealot rhlowe's Avatar
    Join Date
    Sep 2006
    Location
    San Diego
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Figured out the problem was actually something else,

    It turns out that I incorrectly assumed that Internet Explorer included '\n' newlines in a multi-line string. Now I am trying to figure out how to detect line breaks, any ideas?

    Relevant code can be found here:

    http://www.roberthenrylowe.com/lab/j...converter.html
    http://www.roberthenrylowe.com/lab/j...deconverter.js
    San Diego Freelance Web Development Contractor
    Great Taste, no Filler. Now with no Trans Fat!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •