Skip to content
Advertisement

Tag: javascript

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:

Calculate last day of month

If you provide 0 as the dayValue in Date.setFullYear you get the last day of the previous month: There is reference to this behaviour at mozilla. Is this a reliable cross-browser feature or should I look at alternative methods? Answer Output differences are due to differences in the toString() implementation, not because the dates are different. Of course, just because

How do I get a timestamp in JavaScript?

I want a single number that represents the current date and time, like a Unix timestamp. Answer Timestamp in milliseconds To get the number of milliseconds since Unix epoch, call Date.now: Alternatively, use the unary operator + to call Date.prototype.valueOf: Alternatively, call valueOf directly: To support IE8 and earlier (see compatibility table), create a shim for Date.now: Alternatively, call getTime

How to program a text search and replace in PDF files

How would I be able to programmatically search and replace some text in a large number of PDF files? I would like to remove a URL that has been added to a set of files. I have been able to remove the link using javascript under Batch Processing in Adobe Pro, but the link text remains. I have seen recommendations

How to list the properties of a JavaScript object?

Say I create an object thus: What is the best way to retrieve a list of the property names? i.e. I would like to end up with some variable ‘keys’ such that: Answer In modern browsers (IE9+, FF4+, Chrome5+, Opera12+, Safari5+) you can use the built in Object.keys method: The above has a full polyfill but a simplified version is:

How do I unset an element in an array in javascript?

How do I remove the key ‘bar’ from an array foo so that ‘bar’ won’t show up in Answer Don’t use delete as it won’t remove an element from an array it will only set it as undefined, which will then not be reflected correctly in the length of the array. If you know the key you should use splice

Easy way to turn JavaScript array into comma-separated list?

I have a one-dimensional array of strings in JavaScript that I’d like to turn into a comma-separated list. Is there a simple way in garden-variety JavaScript (or jQuery) to turn that into a comma-separated list? (I know how to iterate through the array and build the string myself by concatenation if that’s the only way.) Answer The Array.prototype.join() method:

How to determine equality for two JavaScript objects?

A strict equality operator will tell you if two object types are equal. However, is there a way to tell if two objects are equal, much like the hash code value in Java? Stack Overflow question Is there any kind of hashCode function in JavaScript? is similar to this question, but requires a more academic answer. The scenario above demonstrates

Advertisement