Created
July 28, 2019 07:18
-
-
Save ArtemSites/f9dcb9f2c5217629f3104586b09843a5 to your computer and use it in GitHub Desktop.
Подсчет количества элементов внутри объекта (с числовыми индексами).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
someObject = { | |
0: 'example', | |
1: 'example', | |
2: 'example', | |
returnSize: function() {//Вернуть количество позиций корзины | |
var size = 0; | |
for (key in this) { | |
key = parseInt(key); | |
if (key >= 0) { //Если клююч число то прибавить к счетчику | |
size++; | |
} else if (typeof key == 'NaN') { //Иначе пропустить | |
continue; | |
} | |
} | |
return size; | |
} | |
} | |
var exampleSizeNumberIndexOfObject = someObject.returnSize; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment