Only numeric indices affect the .length of an Array.
Other named properties are allowed, but they aren't the typical use for an Array object. By using "", you're creating a non-numeric property on the object. You can access it like this:
errors[""];
But you can't get to it with the typical Array methods.
For named properties, you'd typically use an Object instead. Either way, you can get a count of the number of own, enumerable properties (including numeric indices) by using Object.keys().
Object.keys(errors).length;
Answer from cookie monster on Stack OverflowOnly numeric indices affect the .length of an Array.
Other named properties are allowed, but they aren't the typical use for an Array object. By using "", you're creating a non-numeric property on the object. You can access it like this:
errors[""];
But you can't get to it with the typical Array methods.
For named properties, you'd typically use an Object instead. Either way, you can get a count of the number of own, enumerable properties (including numeric indices) by using Object.keys().
Object.keys(errors).length;
Because that is not how you add an item since Arrays only accept numeric keys. You do it like this:
errors.push("blah"); /*or*/ errors[0] = "blah";
//Now if you check the length:
errors.length; //1
Also, if you are using it as an Object, '' isn't a valid name either.
*Correction: Looks like you can use "" (empty string) as a key.
brackets - JavaScript array.length remains 0 after elements been pushed in - Stack Overflow
javascript - Array length remains 0 even though I push 'objects' to it - Stack Overflow
javascript - Array.length reading 0 after pushing objects on array - Stack Overflow
jquery - incorrect array length after using array.push() in JavaScript - Stack Overflow
I'm not exactly sure what you're trying to achieve, but you might do something like this:
function UpdBttn(ct){
var array = document.getElementById('array').innerHTML;
var IDarr = array.split(',');
IDarr.push(ct);
document.getElementById('array').innerHTML = IDarr.join(',');
alert(IDarr.length);
}
You need to define your variable outside of the function to prevent it from initializing each time you call the function try this:
var IDarr = [];
function UpdBttn(ct){
var array = document.getElementById('array').innerHTML;
IDarr.push(ct);
document.getElementById('array').innerHTML = IDarr;
alert(IDarr.length);
}
Based on doc https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push
The push() method adds one or more elements to the end of an array and returns the new length of the array.
That's why you get 1 because you only have one element. Since array can be modified by reference, you can do it like this below:
let map = new Map();
map.set("a", []);
map.get("a").push(5);
console.log(map.get('a'))
Another option you can consider is to use immutable approach so the original array doesn't get changed with ES6 spread syntax or concat
map.set("a", [...map.get("a"), 1]);
console.log(map.get('a'));
map.set("a", map.get("a").concat(10));
console.log(map.get('a'));
push() returns the new length of the array, not the array, so that's what you're storing in the Map.
You don't need to use map.set() when you push onto the array. push() updates the array in place, so it modifies the array that's stored in the map. Just write:
let map = new Map();
map.set("a", []);
map.get("a").push(1);
console.log(map.get("a"));
Run code snippetEdit code snippet Hide Results Copy to answer Expand
readdir is asynchronous. It won't get the results right away. You should use the filePaths inside the callback. The only reason why the console shows the value is because the console evaluates the array when you unfold it.
When you press the little arrow on the left, put the mouse on the i box on the right. What happens is that the console keeps a reference to the array, so when the user unfolds the array it then shows the current value of the array. But when you log filePaths.length the array is empty because readdir didn't finish reading yet, that's why you get 0. But by the time you open the console and press that arrow, readdir will have already done reading and the console will print the current value of the array (after it's been filled).

Example to demonstrate the problem: (not a solution, it's just to understand what is really happening)
Open the browser console and try this code and see what happens:
var arr = [];
setTimeout(function() {
arr.push(1, 2, 3);
}, 5000);
console.log(arr.length);
console.log(arr);
Here the array and it's length are both logged before the array is filled. The array will be filled after 5 seconds. So the output will be 0 and a string representation of the array array[]. Now because arrays could have tons of data, the console won't show that data until the user unfolds the array. So what the console does is keep a reference to the array until the user press the unfold arrow. If you unfold the array before 5 seconds you'll see that the array is empty (not filled yet). If you wait until the 5 seconds pass then unfold it, then you'll see that it's filled, even though it was logged as an empty array.
Note: Also, the line that get logged to the console (something like > Array(0)) is just a string representation of the object/array at the moment the log happens. It won't get updated if the object/array changes. So that also may seem confusing sometimes.
I hope it's clear now.
Just to expand on @ibrahim-mahrir 's answer, they means like this
function getPaths() {
var dirPath = document.getElementById("mdir").innerHTML;
var filePaths = [];
fs.readdir(dirPath, function(err, dir) {
for (var i = 0, l = dir.length; i < l; i++) {
var filePath = dir[i];
filePaths.push(dirPath + "/" + filePath);
}
console.log(filePaths);
console.log(filePaths.length);
});
}
MDN: When setting a property on a JavaScript array when the property is a valid array index and that index is outside the current bounds of the array, the engine will update the array's length
The length property is therefore not updating as you're using keys rather than numberical indexes.
Since Array is also an Object, what you're doing is just attaching a property to it, which has no effect on length property whatsoever.
Arrays are objects and can have properties.
var myArray = [];
myArray.push(1);
myArray.push(2);
myArray["somekey"] = [];
A dump give :

The length property is related to the number of elements added with the push method or with a numeric index array[x] =
In this.markerList it seem that you didn't add any element in the array, only added properties.
let raw='https://raw.githubusercontent.com/nshntarora/Indian-Cities-JSON/master/cities.json' var cities=[] fetch(raw) //getting the rawdata from the url .then(response=>response.json()) // returned data is parsed as json file .then(data=>data.forEach(item=>cities.push(item))) //in the json data, each is pushed into cities console.log(cities[0]);//undefined
i am trying to get cities objects in the cities array and try to filter through them. however, whenever i am pushing them to the array, im getting the following output when i console.log(cities):
[]
0: {id: '1', name: 'Mumbai', state: 'Maharashtra'}
1: {id: '2', name: 'Delhi', state: 'Delhi'}
2: {id: '3', name: 'Bengaluru', state: 'Karnataka'}
3: {id: '4', name: 'Ahmedabad', state: 'Gujarat'}
4: {id: '5', name: 'Hyderabad', state: 'Telangana'}
5: {id: '6', name: 'Chennai', state: 'Tamil Nadu'}
6: {id: '7', name: 'Kolkata', state: 'West Bengal'}
7: {id: '8', name: 'Pune', state: 'Maharashtra'}
8: {id: '9', name: 'Jaipur', state: 'Rajasthan'}
9: {id: '10', name: 'Surat', state: 'Gujarat'}
10: {id: '11', name: 'Lucknow', state: 'Uttar Pradesh'}
11: {id: '12', name: 'Kanpur', state: 'Uttar Pradesh'}
12: {id: '13', name: 'Nagpur', state: 'Maharashtra'}
13: {id: '14', name: 'Patna', state: 'Bihar'}
14: {id: '15', name: 'Indore', state: 'Madhya Pradesh'}
15: {id: '16', name: 'Thane', state: 'Maharashtra'}
16: {id: '17', name: 'Bhopal', state: 'Madhya Pradesh'}
17: {id: '18', name: 'Visakhapatnam', state: 'Andhra Pradesh'}
18: {id: '19', name: 'Vadodara', state: 'Gujarat'}
19: {id: '20', name: 'Firozabad', state: 'Uttar Pradesh'}
20: {id: '21', name: 'Ludhiana', state: 'Punjab'}
21: {id: '22', name: 'Rajkot', state: 'Gujarat'}
22: {id: '23', name: 'Agra', state: 'Uttar Pradesh'}
23: {id: '24', name: 'Siliguri', state: 'West Bengal'}why am i not able to access the items if they are already present?