I have read lots and lots of possible solutions all with its drawbacks, e.g. if it from a different frame or if it is a custom object including 'array' in its constructor, etc. but all more than 3 months old. The lattest mentioned Array.isArray([]) that comes with ECMAScript 5th Edition but do not work with IE7 and lower. Wanna know if there is a sure-proof solution for all?
P.S. For my particular application I take some arrays from JSON and want to check that they are arrays and then check teir length property to see if they have data inside
instanceof. toString() is easier to overwrite.
13 3859
since JSON supports only the Object and Array objects, one of the old constructor checks is sufficient
found this page http://perfectionkills.com/instanceo...obust-isarray/
which concludes using -
function isArray(o) {
-
return Object.prototype.toString.call(o) === '[object Array]';
-
}
-
which is my best find so far, question is though that does not say which edition of ECMA-262 he took the definition of Object.prototype.toString (hoping its also working for 3rd edition cause I have tested the function for only the lattest browsers -5th ed)
toString() is a very, very basic method. that’s definitely not a recent addition
for a non framed enviroment (just taking some data from JSON) would u recommend this or instanceof? (have read that though the above is more complete -support multi-framed is slower and more complicated )
framed environment?
I’d use instanceof, the question is which version of IE supports that.
with "framed enviroment" meant working with multiple frames/windows and have to transfer an array from on to an other. From Javascript Bible found:
InstanceOf Compatibility: WinIE5+, MacIE-, NN6+, Moz+, Safari+, Opera+, Chrome+
I don’t think it has an influence on testing for an array.
not sure what u mean, tested this code -
a = new Array(1,2,3);
-
a instanceof Array; //true
-
a instanceof Object; //also true but
-
//I am interesting in testing for arrays
-
which is what i want but read that arrays are also of type object so tested -
function userobject(str1){this.name=str1;}
-
a=new userobject("nikos");
-
alert(a instanceof Array);//false
-
which means that it distinguish object from array, so I am 100% sure of this working right?
P.S. On the other hand if want to test for both arrays and object determine which is which I will have to go with the prev solution right?
any non-primitive (undefined, null & the literals) inherits from Object because Object is the base of every object (including Array) in JavaScript.
So you mean that checking for undefined, null & literals with the InstanceOf Object will return true. But in case of checking with InstanceOf Array all is OK right?
those will return false, as they are not objects.
concluding for the job of checking some arrays in source code (not frames) and/or from JSON u would use instanceOf or Object.prototype.toString.call(o) === '[object Array]' ?
instanceof. toString() is easier to overwrite.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Neil Jarman |
last post by:
Hi,
I need to create an array with a session variable.
According to thebook I'm using, the following should work, but if fails:
for each key in request.QueryString
if left(key,6) = "cboRun"...
|
by: portroe |
last post by:
by the way,
how do you deal with a variable array lenth, for example if the array
depends on the number of years 'input' by the user,
Dim Years(?) As Integer
thanks
|
by: laredotornado |
last post by:
Hi,
Wondered if there was a good one-liner for what I want to do in PHP 4.
I have an array, with an arbitrary number of elements. I'd like to
know if all the elements in the array are empty. ...
|
by: echoking |
last post by:
I have a program that asks a user for a integer. This integer is a member of a struct statement. Later on in the program a function points to this structure in hopes of using the integer as a...
|
by: JasonC |
last post by:
Hi,
First time posting here so please be gentle!
I wish to check a variable for a number of words that are in a array.
Not sure if this is the best way to do it, but this is what i have.
...
|
by: farrell.niall |
last post by:
Hi,
I'm trying to use nested foreach loops to loop through two arrays.
Each element in the first array corresponds to another array. The
brandcode part of the first array is the same as the name...
|
by: javaalien |
last post by:
Kindly please anyone who knows is the any method to know
what is the limit of length of a counter?
Depsite from initiliazed variable's types (either int, byte, long etc)
is there any way for me...
|
by: Keriana30 |
last post by:
I need to base a variable array using a variable. For example,
ReDim pstrDepSSN(pintRecordCount) as string
The only way to do this is with the ReDim statement but it doesn't permit me to...
|
by: wswilson |
last post by:
In python, I could write:
a = 1
if a in :
do something...
In c (and many other languages):
|
by: PauloD |
last post by:
public struct Character_Items
{
public short Armor;
public short Helm;
public short WeaponL;
public short WeaponR;
public short Pants;
public short Boots;
public short Gloves;
public short...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |