I am trying to create an Array of Years. Current year and Last Year. The following code is working however I wanted to see if there is a cleaner way of doing this? Answer Try this
Tag: arrays
Terse way to intersperse element between all elements in JavaScript array?
Say I have an array var arr = [1, 2, 3], and I want to separate each element by an element eg. var sep = “&”, so the output is [1, “&”, 2, “&”, 3]. Another way to think about it is I want to do Array.prototype.join (arr.join(sep)) without the result being a stri…
JavaScript – Store multiple objects in array and access their properties via
So here I have a database of user objects and I would like to push each one into the ‘allUsers’ array. I then want to be able to loop through this array and access a property e.g. allUsers[i].genrePref. How do I push all of these variables into the array to store them like [user, jon, lucy, mike, …
Remove duplicates of array from another array, JavaScript
How can i remove duplicated arrays in this data structure? [![enter image description here][1]][1] I got this: with: I need to reduce data, remove duplicated arrays and provide result to sankey graf. jsonData elements contain much more data and structure of each left, center and right side is a little bit dif…
Combine json arrays by key, javascript
I need to combine two json arrays, delivered by two rest services. The entries with the same “id” belong together. I need a combined/copied/cloned json array in javascript in the following way (my model in angular2): Is there a way to combine them? The parameter names are not defined exactly and i…
Array Reverse is not working for me …
Consider the following code (React JS code): Notice the console.log. Lets see an image: Last I checked, reverse should have reversed the order of the array. Yet it doesn’t. Am I Using this wrong (official MDN Docs)? Why isn’t reverse working? Answer As described at https://developer.mozilla.org/en…
How to return true if all values of array are true otherwise return false?
I have a array like this: Now I want to get true, because all keys of array above are true. another example: Now I need to get false, because there is one false in the array. How can I do that? Answer The shortest code to do this would be arr.every(x => x) or arr.every(function(x) {return x}) for ES5 compa…
How to get first N number of elements from an array
I am working with Javascript(ES6) /FaceBook react and trying to get the first 3 elements of an array that varies in size. I would like do the equivalent of Linq take(n). In my Jsx file I have the following: Then to get the first 3 items I tried This didn’t work as map doesn’t have a set function. …
How to convert array of key–value objects to array of objects with a single property?
I have an array of objects like this: I need to convert it to the following format: How can this be done using JavaScript? Answer You can use .map also if you use ES2015 you can do it like this Example
Group sequential repeated values in Javascript Array
I have this Array: I wish this output: Obs.: Notice that I dont want group all the repeat values. Only the sequential repeated values. Can anyone help me? Answer You can reduce your array like this: see Array.prototype.reduce().