maya90
1
[FONT=Verdana]
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…
[/FONT]
system
2
var myObj = {
year: 2010,
colors : ["red","blue","green","black"]
};
console.log( $.isArray(myObj)); // false
console.log( $.isArray(myObj.colors)); // true
And without jQuery:
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');
};
maya90
3
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)
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:
{ "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…