As i'm trying to drop the first 5 items in the array when array item cross beyond 10. As my array keeps on adding the items in the setInterval method.
function checkArray(array,limit,toshow){
var length = array.length;
if(length > limit){
var splicedArry = _.drop(array1,toshow);
array = splicedArry;
}
return array;
}
Please check the fiddle
Here is the entire working code from your JSfiddle (using Vanilla JS):
var arrayEl = [];
var count =0;
setInterval(function(){
count = ++count;
arrayEl.push(count);
},1000)
setInterval(function() {
// modify array by reference
checkArray(arrayEl,10,5)
// print the contents of modified array
console.log(arrayEl)
}, 1100)
function checkArray(array,limit,toshow){
// splice takes 2 arguments to modify an array (people often confuse it with `slice`
if (array.length > limit) array.splice(0, toshow);
}
Lodash _.drop
creates a new array, so the entire code code would look like this:
var arrayEl = [];
var count =0;
setInterval(function(){
count = ++count;
arrayEl.push(count);
},1000)
setInterval(function() {
// you must assign the returned value to your original array
arrayEl = checkArray(arrayEl,10,5)
console.log(arrayEl)
}, 1100)
function checkArray(array,limit,toshow){
if (array.length > limit) return _.drop(array, toshow);
return array;
}