.each does not work in the array. But .ISArray returns true?

I created an object that contains several elements, including one that contains several objects, each of which contains an array. This is how it is structured.

$.myVar = {
    cp : "",
    ps : {
        m1 : ["001", "002", "003"],
        m2 : ["002", "004"]
    }
};

My scripts continue to crash, saying that $ .myVar.ps ["m1"] does not have a method each.

When I hit the Chrome console for research, I run the following and get the displayed output.

$.myVar.ps["m1"]
["001", "002", "003"]
$.myVar.ps["m1"].each( function (i, p) {alert(i)})
TypeError: Object 001,002,003 has no method 'each'

Also, if I run the following, this proves that m1 is an array.

$.isArray($.myVar.ps["m1"])
true

So it looks like m1 is an array, but he refuses to treat it as such. Any idea what I'm doing wrong?

+5
source share
3 answers

each Array; jQuery, .. $.

$($.myVar.ps.m1).each(function (i, el) { /* ... */ });

( , jQuery, $(...)), $.each:

$.each($.myVar.ps.m1, function (i, el) { /* ... */ });

, (IE >= 9) es5-shim, Array.prototype.forEach:

$.myVar.ps.m1.forEach(function (el, i) { /* ... */ });

(IMO , , ).

+19

.each jQuery. Javascript " " $.each.

$.each($.myVar.ps["m1"], function(i,p) { alert(i); });
+7

javascript. :

$($.myVar.ps["m1"]).each
-2

All Articles