Skip to content
Advertisement

Tag: arrays

How to iterate array keys in Javascript?

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.

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

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

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:

Advertisement