Take a look at Array.slice(begin, end)
const ar = [1, 2, 3, 4, 5];
// slice from 1..3 - add 1 as the end index is not included
const ar2 = ar.slice(1, 3 + 1);
console.log(ar2); // -> [2, 3, 4]
// the original ar is unmodified.
console.log(ar); // -> [1, 2, 3, 4, 5]
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Answer from Alex K. on Stack OverflowTake a look at Array.slice(begin, end)
const ar = [1, 2, 3, 4, 5];
// slice from 1..3 - add 1 as the end index is not included
const ar2 = ar.slice(1, 3 + 1);
console.log(ar2); // -> [2, 3, 4]
// the original ar is unmodified.
console.log(ar); // -> [1, 2, 3, 4, 5]
Run code snippetEdit code snippet Hide Results Copy to answer Expand
For a simple use of slice, use my extension to Array Class:
Array.prototype.subarray = function(start, end) {
if (!end) { end = -1; }
return this.slice(start, this.length + 1 - (end * -1));
};
Then:
var bigArr = ["a", "b", "c", "fd", "ze"];
Test1:
bigArr.subarray(1, -1);
< ["b", "c", "fd", "ze"]
Test2:
bigArr.subarray(2, -2);
< ["c", "fd"]
Test3:
bigArr.subarray(2);
< ["c", "fd","ze"]
Might be easier for developers coming from another language (i.e. Groovy).
How to set subarray elements?
javascript - Creating subarrays in array - Stack Overflow
javascript - How to create sub array from array - Stack Overflow
How do I remove from an array those elements that exists in another array?
The solution
let array = [ 1, 1, 1, 1, 2, 2, 2, 4, 5, 10, 20, 20, 391, 392, 591 ];
let temp = {};
array.forEach( el => {
if(temp[el] != null){
temp[el].push(el)
}else{
temp[el] = [el]
}
})
let output = Object.values(temp).map(e=>e[1]?e:e[0]);
console.log(output)
The Funny version :)
__={};
[ 1, 1, 1, 1, 2, 2, 2, 4, 5, 10, 20, 20, 391, 392, 591 ]
.forEach(_=>__[_]?__[_].push(_):__[_]=[_]);
__=Object.values(__).map(_=>_[1]?_:_[0]);
console.log(__);
Disclaimer: this snippet is here only for entertainment purposes, I strongly discourage you to use it in production
sort() the numbers, reduce() to arrays, and map() to take the numbers that occur a single time out of their array:
let arr = [ 1, 1, 1, 1, 2, 2, 2, 4, 5, 10, 20, 20, 391, 392, 591 ]
let sort = arr.sort((a,b) => a - b)
let res = sort.reduce((acc,cur) => {
if(acc[acc.length-1] && cur === acc[acc.length-1][0]){
acc[acc.length-1].push(cur)
}else{
acc.push([cur])
}
return acc
},[]).map(el => el.length === 1 ? el[0] : el)
console.log(res)
Array#reduce the array into an array of arrays by using a object as a hash:
var arr = ['2016-12-16', '2016-12-16', '2016-12-17', '2016-12-17', '2016-12-17', '2016-12-17', '2016-12-18', '2016-12-18', '2016-12-19'];
var result = arr.reduce(function(r, item) {
(r.hash[item] || (r.hash[item] = r.arr[r.arr.push([]) - 1])).push(item);
return r;
}, { arr: [], hash: {} }).arr;
console.log(result);
The easiest way to do this is with a dictionary (which JS objects naturally act as).
var datesArray = [ "2016-12-16", "2016-12-16", "2016-12-17", "2016-12-17", "2016-12-17", "2016-12-17", "2016-12-18", "2016-12-18", "2016-12-19" ]; //I've corrected the syntax for this to make each date a string, rather than a sequence of subtractions
var datesCount = {};
datesArray.forEach(function(date){
if(!datesCount[date])
datesCount[date] = []
datesCount[date].push(date);
});
At this point, you have a set of group arrays, each containing every instance of a particular date. You can iterate over it with Object.getOwnPropertyNames and put each array into the larger array OR you could just use it directly.