796
votes
19answers
455k 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++) { ...
499
votes
8answers
249k views

Appending to array

How do I append to an array in Javascript?
427
votes
14answers
350k 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 ...
415
votes
3answers
185k 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?
380
votes
16answers
174k 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. Why not?
377
votes
21answers
247k 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 ...
361
votes
15answers
225k 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 ...
336
votes
13answers
357k 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 ...
311
votes
9answers
325k 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; ...
299
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?
253
votes
18answers
144k 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 ...
241
votes
4answers
126k 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 ...
210
votes
13answers
250k views

For each in an array. How to do that in JavaScript?

I have an array of a varying number of objects. At a certain point, I want to do something to all the objects in that array. How do I do this with JavaScript? I thought of something like this: ...
197
votes
8answers
106k 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 ...
177
votes
7answers
80k views

Sorting an array of JavaScript objects

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

1 2 3 4 5 399
15 30 50 per page