744
votes
18answers
426k views

array.contains(obj) in JavaScript

What is the most concise and efficient way to find out if a JavaScript array contains an obj? This is the only way I know to do it: contains(a, obj) { for (var i = 0; i < a.length; i++) { ...
466
votes
8answers
229k views

Appending to array

How do I append to an array in Javascript?
407
votes
13answers
328k views

JavaScript Array Delete Elements

What is the difference between using the delete operator on the array element as opposed to using the Array.splice method? For example: myArray = ['a', 'b', 'c', 'd']; delete myArray[1]; // or ...
391
votes
3answers
173k views

Checking if an associative array key exists in Javascript

How do I check if a particular key exists in a Javascript associative array? If a key doesn't exist and I try to access it, will it return false? Or throw an error?
353
votes
21answers
235k views

What is the best way to add options to a select from an array with jQuery?

What is the best method for adding options to a select from a JSON object using jQuery? I'm looking for something that I don't need a plugin to do, but would also be interested in the plugins that ...
347
votes
16answers
164k views

Why is using “for…in” with array iteration such a bad idea?

I've been told not to use "for...in" with Arrays in JavaScript but never received a satisfactory reason as to why not.
341
votes
14answers
210k views

Length of Javascript Object (ie. Associative Array)

If I have a javascript associative array say: var myArray = new Object(); myArray["firstname"] = "Gareth"; myArray["lastname"] = "Simpson"; myArray["age"] = 21; Is there a built in or accepted best ...
300
votes
13answers
320k views

Loop through array in JavaScript

In Java you can use a for() loop to go through objects in an array like so: String[] myStringArray = {"Hello","World"}; for(String s : myStringArray) { //Do something } can you do the same in ...
297
votes
9answers
310k views

Best way to find an item in a JavaScript array? [duplicate]

Possible Duplicate: array.contains(obj) in JavaScript What is the best way to find if an object is in an array? This is the best way I know: function include(arr, obj) { for(var i=0; ...
294
votes
12answers
18k views

Why does [1,2] + [3,4] = “1,23,4” in JavaScript?

I wanted to add the elements of an array into another, so I tried this simple sentence in our beloved Firebug: [1,2] + [3,4] It responded with: "1,23,4" What is going on?
222
votes
18answers
127k views

JavaScript: Check if object is array?

I'm trying to write a function that either accepts a list of strings, or a single string. If it's a string, then I want to convert it to an array with just the one item. Then I can loop over it ...
219
votes
4answers
116k views

Javascript - Insert Item into Array at a Specific Index

I am looking for a JavaScript array insert method, in the style of: arr.insert(index, item) Preferably in jQuery, but any JavaScript implementation will do at this point because I can't believe the ...
183
votes
8answers
100k views

How do I remove objects from a javascript associative array?

Suppose I have this code: var myArray = new Object(); myArray["firstname"] = "Bob"; myArray["lastname"] = "Smith"; myArray["age"] = 25; Now if I wanted to remove "lastname"?....is there some ...
162
votes
12answers
69k views

How do you check if a variable is an array in JavaScript?

I would like to check whether a variable is either an array or a single value in JavaScript. I have found a possible solution... if (variable.constructor == Array)... Is this the best way this can ...
161
votes
8answers
75k views

Sorting an array of JavaScript objects

I read the following JSON using Ajax and stored the objects in an array: var homes = [ { "h_id": "3", "city": "Dallas", "state": "TX", "zip": "75201", ...

1 2 3 4 5 375
15 30 50 per page