function deleteNth(arr,x) {
//Define a cache object;
var cache = {};
return arr.filter(function(n) {
//The filter() method creates an array filled with all array elements that pass a test (provided as a function)//////
cache[n] = (cache[n]||0) + 1;
//The function returns every array element that is equal or smaller than x//////
return cache[n] <= x;
});
}
The filter function takes each element of the array, and assigns its value to N.
It then increments a property of the cache object corresponding to that value.
If the cache property’s value (the count of occurances of value N) is less than or equal to x, the value is allowed to remain in the array, otherwise it is filtered out.
(Though its not strictly that, as it technically means “if (anything-that-evaluates-to-false) {”, but because the object was created empty inside the function, we can be sure that the properties of the object are either undefined or a non-zero number.)
The idea being you cant add 1 to an undefined property, so it substitutes a 0 if there would be an undefined there.
function should probably be named something else, as “deleteNth” sounds like it’s hunting specifically for the Nth, not more-than-Nth…