array.length isn't necessarily the number of items in the array: var a = ['car1', 'car2', 'car3']; a[100] = 'car100'; a.length; // 101 The length of the array is one more than the highest index. jesussssss !!!
so how to get actual length of array ? @TRGWII
Language: js Code: const actualLength = (arr) => { let length = 0; for (let i = 0; i < arr.length; i++) { if (i in arr) length++; } return length; }; var a = ['car1', 'car2', 'car3']; a[100] = 'car100'; console.log(actualLength(a)); Output: 4
yeah I know we can loop over that :)) also found this Object.keys(array).length
Please stop using var. It was deprected 7 years ago. Use const or let instead
Обсуждают сегодня