How do you filter out no more than 2 of the same item on a list of multiple items? #javascript
How do you filter out no more than 2 of the same item on a list of multiple items?
#javascripttutorial #coding #filter
***SAMPLE FUNCTION:
function filterList(arr) {
var finalList = [];
return arr.filter(function(n) {
finalList[n] = (finalList[n]||0) + 1;
return finalList[n] **less than or equal to** 2;
});
}
console.log(filterList([1,2,1,2,1,2,2]))
// output: [ 1, 2, 1, 2 ]
console.log(filterList([3,2,5,5,5]))
// output: [ 3, 2, 5, 5 ]
#Shorts #youtubeShorts #trending #viral #quicktips #youtube #youtuber #Shortstiktok
Видео How do you filter out no more than 2 of the same item on a list of multiple items? #javascript канала Let's Code!
#javascripttutorial #coding #filter
***SAMPLE FUNCTION:
function filterList(arr) {
var finalList = [];
return arr.filter(function(n) {
finalList[n] = (finalList[n]||0) + 1;
return finalList[n] **less than or equal to** 2;
});
}
console.log(filterList([1,2,1,2,1,2,2]))
// output: [ 1, 2, 1, 2 ]
console.log(filterList([3,2,5,5,5]))
// output: [ 3, 2, 5, 5 ]
#Shorts #youtubeShorts #trending #viral #quicktips #youtube #youtuber #Shortstiktok
Видео How do you filter out no more than 2 of the same item on a list of multiple items? #javascript канала Let's Code!
Комментарии отсутствуют
Информация о видео
17 августа 2024 г. 1:02:55
00:00:13
Другие видео канала