Array.prototype.reverse reverses the contents of an array in place (with mutation)… Is there a similarly simple strategy for reversing an array without altering the contents of the original array (without mutation)? Answer You can use slice() to make a copy then reverse() it
Tag: arrays
Merge duplicate objects in array of objects
I have below array of objects, I want to merge the duplicate objects based on attribute ‘label’ so that Final output will look like below, Can someone help me identify the approach? Answer I would probably loop through with filter, keeping track of a map of objects I’d seen before, along the…
Iterative solution for flattening n-th nested arrays in Javascript
Can anyone show me an iterative solution for the following problem? I solved it recursively but struggled with an iterative solution. (Facebook Technical Interview Question) Solution must work with n-th nested array elements (i.e. it must still work if someone modifies the array values/placement in the exampl…
How to access first element of JSON object array?
I exptect that mandrill_events only contains one object. How do I access its event-property? Answer To answer your titular question, you use [0] to access the first element, but as it stands mandrill_events contains a string not an array, so mandrill_events[0] will just get you the first character, ‘[&#…
What is the pattern used by Google Tag Manager in order to observe de `dataLayer` Array?
I observed the dataLayer array and I don’t see any changes to push. No custom methods at all, actually. How is GTM observing the changes to the array? As far as I know, changes to an Array don’t throw any events, do they? Edit: I did some more research and found Google’s library for interact…
Eloquent Javascript Chapter 4: arrayToList/listToArray Execise
Question from eloquentjavascript: Write a function arrayToList that builds up a data structure like the previous one when given [1, 2, 3] as argument, and write a listToArray function that produces an array from a list. Also write the helper functions prepend, which takes an element and a list and creates a n…
How to get the last record in an array in AngularJS
I have an array of objects ObjectsArr= [Object1 , Object2,Object3, Object4] I want to show in my view the last object, how to do that in angularjs? Answer ObjectsArr[ObjectsArr.length – 1] — this will give you the last element in the array. To display it in view: {{ObjectsArr[ObjectsArr.length …
Concatenate Object values
I have a JavaScript Object and I’m sure the value of any key is an array (even empty in some case): Aside from using Underscore, is there any way to concatenate all the values of this Object (and create a new array)? At the moment I get the keys name using Object.keys, then I loop and concatenate. Any h…
Sorting multiple arrays based on one
I have 2 arrays: the first one contains a list of score numbers, and the second contains a list of countries with the same number of elements. Each country is linked with its proper score during the input. So, let’s say the input arrays are the following: I need the following output: A possible solution…
In JavaScript, what is the simplest way to insert text into a string?
I have an issue where I need to take a string (which is a query string) in JavaScript and transform it into another query string (to avoid clashes). The original string I have comes in as: And I want to take a prefix (in my case it will be something like “slides[0].” and put it in front of all of