Don''t iterate over the properties of an aray using a for ... in loop.
See
http://andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/
for more details, and use Array#each instead.
Regards,
Tobie
On Sep 21, 8:13 pm, Joseph_cjis
<josephlnicho...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I am trying to have prototype perform a request and return to me a
> javascript object representing the json string returned by the
> request. I want to iterate over the contents to print each object in
> the json string. The value of transport.reponseText in onSuccess is
> what I would expect. But once I try to perform evalJSON() on this
> text it gives me a bunch of extra function()s when i try to iterate
> over the result. Why does the object contain these function()s and
> how can I iterate over just my objects?
>
> Below is my code along with the strange result I am seeing.
>
> thank you for your insights.
>
> new Ajax.Request(''loadSeveritiesMatchingCategory.action'',
> {
> method:''get'',
> parameters : ''successType=jsonSuccess&categoryId=''
+ categoryId,
> onSuccess: function(transport)
> {
> var json = transport.responseText.evalJSON();
> var htmlSeverities = transport.responseText +
''<p/>'';
> for (x in json)
> {
> htmlSeverities+= json[x].toString()
+''<p/>'';//''Severity: '' +
> json[x].name + '' Priority: '' + json[x].priority +
''<br/>'';
> }
> $(''ajaxResults'').innerHTML = htmlSeverities;
> }
>
> });
>
> contents of ajaxResults div
> -----------------------------------
>
>
[{"id":20,"name":"unknown","priority":1},{"id":8,"name":"non-
>
critical.","priority":5},{"id":17,"name":"medium","priority":15},{"id":
>
18,"name":"severe","priority":19},{"id":
> 19,"name":"critical","priority":25}]
>
> [object Object]
>
> [object Object]
>
> [object Object]
>
> [object Object]
>
> [object Object]
>
> function (iterator) { var index = 0; try { this._each((function
> (value) {iterator(value, index++);})); } catch (e) { if (e != $break)
> { throw e; } } return this; }
>
> function (number, iterator) { var index = -number, slices = [], array
> = this.toArray(); while ((index += number) < array.length)
> { slices.push(array.slice(index, index + number)); } return
> slices.map(iterator); }
>
> function (iterator) { var result = true; this.each((function (value,
> index) {result = result && !!(iterator || Prototype.K)(value,
> index);if (!result) {throw $break;}})); return result; }
>
> function (iterator) { var result = false; this.each((function (value,
> index) {if (result = !!(iterator || Prototype.K)(value, index)) {throw
> $break;}})); return result; }
>
> function (iterator) { var results = []; this.each((function (value,
> index) {results.push((iterator || Prototype.K)(value, index));}));
> return results; }
>
> function (iterator) { var result; this.each((function (value, index)
> {if (iterator(value, index)) {result = value;throw $break;}})); return
> result; }
>
> function (iterator) { var results = []; this.each((function (value,
> index) {if (iterator(value, index)) {results.push(value);}})); return
> results; }
>
> function (pattern, iterator) { var results = []; this.each((function
> (value, index) {var stringValue = value.toString();if
> (stringValue.match(pattern)) {results.push((iterator || Prototype.K)
> (value, index));}})); return results; }
>
> function (object) { var found = false; this.each((function (value) {if
> (value == object) {found = true;throw $break;}})); return found; }
>
> function (number, fillWith) { fillWith = fillWith === undefined ?
> null : fillWith; return this.eachSlice(number, (function (slice)
> {while (slice.length < number) {slice.push(fillWith);}return
> slice;})); }
>
> function (memo, iterator) { this.each((function (value, index) {memo >
iterator(memo, value, index);})); return memo; }
>
> function (method) { var args = $A(arguments).slice(1); return
> this.map((function (value) {return value[method].apply(value,
> args);})); }
>
> function (iterator) { var result; this.each((function (value, index)
> {value = (iterator || Prototype.K)(value, index);if (result => undefined
|| value >= result) {result = value;}})); return result; }
>
> function (iterator) { var result; this.each((function (value, index)
> {value = (iterator || Prototype.K)(value, index);if (result => undefined
|| value < result) {result = value;}})); return result; }
>
> function (iterator) { var trues = [], falses = []; this.each((function
> (value, index) {((iterator || Prototype.K)(value, index) ? trues :
> falses).push(value);})); return [trues, falses]; }
>
> function (property) { var results = []; this.each((function (value,
> index) {results.push(value[property]);})); return results; }
>
> function (iterator) { var results = []; this.each((function (value,
> index) {if (!iterator(value, index)) {results.push(value);}})); return
> results; }
>
> function (iterator) { return this.map((function (value, index) {return
> {value:value, criteria:iterator(value, index)};})).sort((function
> (left, right) {var a = left.criteria, b = right.criteria;return a <
> b ? -1 : a > b ? 1 : 0;})).pluck("value"); }
>
> function () { return [].concat(this); }
>
> function () { var iterator = Prototype.K, args = $A(arguments); if
> (typeof args.last() == "function") { iterator = args.pop(); } var
> collections = [this].concat(args).map($A); return this.map((function
> (value, index) {return iterator(collections.pluck(index));})); }
>
> function () { return this.length; }
>
> function () { return "[" + this.map(Object.inspect).join(",
") +
> "]"; }
>
> function (iterator) { var result; this.each((function (value, index)
> {if (iterator(value, index)) {result = value;throw $break;}})); return
> result; }
>
> function (iterator) { var results = []; this.each((function (value,
> index) {if (iterator(value, index)) {results.push(value);}})); return
> results; }
>
> function (object) { var found = false; this.each((function (value) {if
> (value == object) {found = true;throw $break;}})); return found; }
>
> function () { return this.map(); }
>
> function reverse() { [native code] }
>
> function (iterator) { for (var i = 0, length = this.length; i <
> length; i++) { iterator(this[i]); } }
>
> function () { this.length = 0; return this; }
>
> function () { return this[0]; }
>
> function () { return this[this.length - 1]; }
>
> function () { return this.select((function (value) {return value !>
null;})); }
>
> function () { return this.inject([], (function (array, value) {return
> array.concat(value && value.constructor == Array ? value.flatten()
:
> [value]);})); }
>
> function () { var values = $A(arguments); return this.select((function
> (value) {return !values.include(value);})); }
>
> function () { return this.length > 1 ? this : this[0]; }
>
> function (sorted) { return this.inject([], (function (array, value,
> index) {if (0 == index || (sorted ? array.last() != value : !
> array.include(value))) {array.push(value);}return array;})); }
>
> function () { return [].concat(this); }
>
> function () { var results = []; this.each((function (object) {var
> value = Object.toJSON(object);if (value !== undefined)
> {results.push(value);}})); return "[" + results.join(",
") + "]"; }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---