I have an array created with this code: I want to get each of the values 46, 66, 90 in a loop. I tried for (var key in widthRange) but this gives me a whole bunch of extra properties (I assume they are functions on the object). I can’t use a regular for loop since the values are not sequential.
Tag: arrays
How to set DOM element as first child?
I have an element E and I’m appending some elements to it. All of a sudden, I find out that the next element to append should be the first child of E. What’s the trick, how to do it? Method unshift doesn’t work because E is an object, not array. Long way would be to iterate through E’s children and
How to remove element from an array in JavaScript?
Remove the first element I want to remove the first element of the array so that it becomes: Remove the second element To extend this question, what if I want to remove the second element of the array so that it becomes: Answer For a more flexible solution, use the splice() function. It allows you to remove any item in
How might I find the largest number contained in a JavaScript array?
I have a simple JavaScript Array object containing a few numbers. Is there a function that would find the largest number in this array? Answer Resig to the rescue: Warning: since the maximum number of arguments is as low as 65535 on some VMs, use a for loop if you’re not certain the array is that small.
How to create an array of object literals in a loop?
I need to create an array of object literals like this: In a loop like this: The value of key should be results[i].label in each element of the array. Answer
Why can I add named properties to an array as if it were an object?
The following two different code snippets seem equivalent to me: and because they both behave the same, and also typeof(myArray) == typeof(myObjects) (both yield ‘object’). Is there any difference between these variants? Answer Virtually everything in javascript is an object, so you can “abuse” an Array object by setting arbitrary properties on it. This should be considered harmful though. Arrays
Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array
I need to check a JavaScript array to see if there are any duplicate values. What’s the easiest way to do this? I just need to find what the duplicated values are – I don’t actually need their indexes or how many times they are duplicated. I know I can loop through the array and check all the other values
Associative arrays in javascript
I have this object: Its used in this way: The problem is, in the addField() function the fields array is being set correct (perhaps because a numerical index is being used to refer to it) but the other 2 arrays (labels and rules) aren’t being touched at all. Doing a console.log shows them as empty in firebug. What do I
How to append something to an array?
This question’s answers are a community effort. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. How do I append an object (such as a string or number) to an array in JavaScript? Answer Use the Array.prototype.push method to append values to the end of an array: You can use the push() function
Javascript Array.sort implementation?
Which algorithm does the JavaScript Array#sort() function use? I understand that it can take all manner of arguments and functions to perform different kinds of sorts, I’m simply interested in which algorithm the vanilla sort uses. Answer I’ve just had a look at the WebKit (Chrome, Safari …) source. Depending on the type of array, different sort methods are used: