Skip to content
Advertisement

Merge keys array and values array into an object in JavaScript

I have: And I’d like to convert it into this object: In Python, there’s the simple idiom dict(zip(keys,values)). Is there something similar in jQuery or plain JavaScript, or do I have to do this the long way? Answer Simple JS function would be: Of course you could also actually implement functions like zip, etc as JS supports higher order types

Getting the client’s time zone (and offset) in JavaScript

How can I gather the visitor’s time zone information? I need both: the time zone (for example, Europe/London) and the offset from UTC or GMT (for example, UTC+01) Answer Using getTimezoneOffset() You can get the time zone offset in minutes like this: The time-zone offset is the difference, in minutes, between UTC and local time. Note that this means that

JavaScript – href vs onclick for callback function on Hyperlink

I want to run a simple JavaScript function on a click without any redirection. Is there any difference or benefit between putting the JavaScript call in the href attribute (like this): vs. putting it in the onclick attribute (binding it to the onclick event)? Answer Putting the onclick within the href would offend those who believe strongly in separation of

Get the element with the highest occurrence in an array

I’m looking for an elegant way of determining which element has the highest occurrence (mode) in a JavaScript array. For example, in the ‘apple’ element is the most frequent one. Answer This is just the mode. Here’s a quick, non-optimized solution. It should be O(n).

Advertisement