2009/5/29 dgalvez <[email protected]>: > > the point is, sometimes i receive a json object like > > var json = { > element: ['1','2','3'] > } > > and sometimes element has just one element so it is presented as > a String not an Array: > var json = { > element: '1' > } > > And i want to iterate over json.element independently of its > type, i normally do something like: > > objectToArray(json.element).each(function(ele){ ... }.bind > (this)); > > where objectToArray is something like this: > > function objectToArray(obj) { > if (!Object.isArray(obj) && !Object.isHash(obj)) { > var objArray = [obj]; > return objArray; > } > return obj; > } > So my question is, it is possible doing that by adding the each > () method to the Object class, > and making it do something like objectToArray(this) before > iterating over the this object? > > Sorry if i haven´t explained well. > > > > > > >
What is creating the JSON data? Can that be told to generate an array for a single entity. I use PHP, so a 1 liner like this ... $element = is_array($element) ? $element : array($element); will take $element and make it an array if it isn't already. Then ... $JSON = json_encode($element); -- ----- Richard Quadling Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498&r=213474731 "Standing on the shoulders of some very clever giants!" --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---
