| 1. |
If We Have A Javascript Associative Array Var Counterarray = { A : 3, b : 4 }; Counterarray["c"] = 1; how Can We Calculate The Length Of The Above Associative Array's Counter Array? |
|
Answer» There are no in-built functions and properties AVAILABLE to calculate the length of associative array object here. However, there are other ways by which we can calculate the length of an associative array object. In addition to this, we can also extend an Object by adding a method or PROPERTY to the prototype in order to calculate length. However, extending an object might break enumeration in various libraries or might create cross-browser issues, so it's not recommended unless it's necessary. Again, there are various ways by which we can calculate length. Object has the keys method which can be used to calculate the length of an object: Object.keys(counterArray).length // Output 2 We can also calculate the length of an object by iterating through an object and by counting the object's own property. function getSize(object) There are no in-built functions and properties available to calculate the length of associative array object here. However, there are other ways by which we can calculate the length of an associative array object. In addition to this, we can also extend an Object by adding a method or property to the prototype in order to calculate length. However, extending an object might break enumeration in various libraries or might create cross-browser issues, so it's not recommended unless it's necessary. Again, there are various ways by which we can calculate length. Object has the keys method which can be used to calculate the length of an object: Object.keys(counterArray).length // Output 2 We can also calculate the length of an object by iterating through an object and by counting the object's own property. function getSize(object) |
|