Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var myArray2 =[];
- myArray2[10]='x';
- myArray2[55]='x';
- Array.prototype.trueLength= function(){
- for(var i = 0,ctr=0,len=myArray2.length;i<len;i++){
- if(myArray2[i]!=undefined){
- ctr++;
- }
- }
- return ctr;
- }
- console.log(myArray2.trueLength());
- var length = myArray2.reduce(function(sum) {
- return sum+1;
- }, 0);
- var length = myArray2.filter(function(item, idx) {
- return idx in myArray2;
- }).length;
- Object.keys(myArray2).length;
- for (var length = 0, i = 0; i < myArray2.length; i++) {
- if (i in myArray2) {
- length += 1;
- }
- }
- Array.prototype.trueLength= function(){
- var ctr = 0;
- for(var i in this){
- if(this.hasOwnProperty(i)){
- ctr++;
- }
- }
- return ctr;
- }
- console.log(myArray2.trueLength());
- Array.prototype.trueLength= function(){
- var list= [], ctr = 0, array = this;
- for(var i in array) (function(arr) {
- if(array.hasOwnProperty(i)){
- list.push(arr);
- ctr++;
- };
- }(array[i]));
- return {length: ctr, "list": list}
- }
- var myArray2 =[];
- myArray2[10]='44';
- myArray2[55]='55';
- // list not undefined
- myArray2.trueLength().list // ["44", "55"]
- // not undefined length list
- myArray2.trueLength().length // 2
- function getArrayPropertyCount(arr) {
- var count = 0;
- for (var i = 0, len = arr.length; i < len; ++i) {
- if (i in arr) {
- ++count;
- }
- }
- return count;
- }
Add Comment
Please, Sign In to add comment