SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    825
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    checking whether an obj is an array......


    hi,

    I'm parsing a JSON (http://mayacove.com/dev/json/json_test.txt)

    and I'm having a hard time determining whether an obj is an array or not...

    found this jQuery method $.isArray(obj), but it's not working....

    my example is here,
    http://mayacove.com/dev/json/json_test.html


    also tried this

    if (obj.length)

    but also didn't work...

    I imagine this is something that has to be done quite commonly when parsing JSON..

    what is best method for determining whether an object is an array, please...

    thank you...



  2. #2
    Non-Member
    Join Date
    Feb 2012
    Posts
    892
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Code:
    var myObj = {
        year: 2010,
        colors : ["red","blue","green","black"]
    };
    
    console.log( $.isArray(myObj)); // false
    console.log( $.isArray(myObj.colors)); // true
    And without jQuery:
    Code:
    var myObj = {
        year: 2010,
        colors : ["red","blue","green","black"]
    };
    
    if( Object.prototype.toString.call(myObj) === '[object Array]' ) {
        console.log('true');
    } else {
        console.log('false');    
    };
    
    if( Object.prototype.toString.call(myObj.colors) === '[object Array]' ) {
        console.log('true');
    } else {
        console.log('false');    
    };

  3. #3
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    825
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    thank you for your response...

    then why is my conditional not working here?
    http://mayacove.com/dev/json/json_test.html
    (you need to click on one of the stars to see prod info)

    Code:
    	if (typeof propVal2 === 'string') {
    		console.log('string');
    		contentProd += '<li>' + propKey2 + ': ' + propVal2 + '</li>';
    	} else {
    	//	if ( typeof propVal2 != 'string' && propVal2.length ) {	
    		if ( $.isArray(propVal2) ) {	// not being met
    			console.log('array');
    			ccolors = propVal2.split(', ').join(", ");
    			contentProd += '<li> arr -- colors: ' + ccolors + '</li>';	
    			
    		}
            }

    JSON:

    Code:
    { "products" : [ 
    	
    		{ "camera":
    			{"price": "$890.00", 
    			 "brand": "Nikon",
    			 "model": "D700",
    			 "more info" : {
                    "from": "Japan",
                    "weight": "4 lbs",
                    "year": 2012,
    				"colors" : ["red","blue","green","black"]
    			 }
    			}
    		},
    		
    		{ "laptop":
    			{"price": "$980.00", 
    			 "brand": "Toshiba",
    			 "model": "L989",
    			 "more info" : {
                    "from": "South Korea",
                    "weight": "5 lbs",
                    "year": 2011,
    				"colors" : ["red","blue","green","black"]
    			 }
    			}
    		}
    		
    		
    	]
    }
    (showing just two records here....)

    thank you...


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
  •