Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.each not working on an Array. But .isArray returns true?

I have created an object which contains a few items, including one which contains multiple objects each of which contains an array. Here is how it is structured.

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

My scripts keep crashing saying that $.myVar.ps["m1"] has no method each.

When I got into Chrome's console to investigate, 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, it proves that m1 is an array.

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

So it seems to agree with m1 is an array, but it refuses to treat it as such. Any idea what I'm doing wrong?

like image 526
McB Avatar asked Nov 24 '10 19:11

McB


People also ask

What does isarray return in JavaScript?

IsArray returns True if the variable is an array; otherwise, it returns False. IsArray is especially useful with variants containing arrays. This example uses the IsArray function to check if a variable is an array.

What is the difference between array isarray A1 and A2?

Then Array.isArray (a1) returns true, but Array.isArray (a2) returns false. What is happening here? Could you please explain how this is happening? Show activity on this post. The Object.create () method creates a new object with the specified prototype object and properties.

What is the difference between isarray and varname in JavaScript?

The required varname argument is an identifier specifying a variable. IsArray returns True if the variable is an array; otherwise, it returns False. IsArray is especially useful with variants containing arrays.

How to test if an array is an array in JavaScript?

The arr.isArray () method determines whether the value passed to this function is an array or not. This function returns true if the argument passed is array else it returns false. Parameters: This method accept a single parameter as mentioned above and described below: obj: This parameter holds the object that will be tested.


2 Answers

each is not a native Array method; it is a method of jQuery objects, i.e. those created by the $ function. You can either do

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

(not recommended, because it creates an unnecessary jQuery object when it wraps the array in $(...)) or you can just use $.each:

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

The most recommended route, if you are using a modern browser (IE >=9), or using es5-shim, is to use the standard Array.prototype.forEach method:

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

Note the different argument order (IMO better since you can then leave out the index if you don't need it).

like image 73
Domenic Avatar answered Oct 17 '22 13:10

Domenic


.each is only defined for jQuery objects. For pure Javascript arrays, use the "static method" $.each.

$.each($.myVar.ps["m1"], function(i,p) { alert(i); });
like image 34
kennytm Avatar answered Oct 17 '22 12:10

kennytm