Guest User

Untitled

a guest
Jan 19th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.47 KB | None | 0 0
  1. var myArray2 =[];
  2. myArray2[10]='x';
  3. myArray2[55]='x';
  4.  
  5. Array.prototype.trueLength= function(){
  6. for(var i = 0,ctr=0,len=myArray2.length;i<len;i++){
  7. if(myArray2[i]!=undefined){
  8. ctr++;
  9. }
  10. }
  11. return ctr;
  12. }
  13. console.log(myArray2.trueLength());
  14.  
  15. var length = myArray2.reduce(function(sum) {
  16. return sum+1;
  17. }, 0);
  18.  
  19. var length = myArray2.filter(function(item, idx) {
  20. return idx in myArray2;
  21. }).length;
  22.  
  23. Object.keys(myArray2).length;
  24.  
  25. for (var length = 0, i = 0; i < myArray2.length; i++) {
  26. if (i in myArray2) {
  27. length += 1;
  28. }
  29. }
  30.  
  31. Array.prototype.trueLength= function(){
  32. var ctr = 0;
  33. for(var i in this){
  34. if(this.hasOwnProperty(i)){
  35. ctr++;
  36. }
  37. }
  38. return ctr;
  39. }
  40. console.log(myArray2.trueLength());
  41.  
  42. Array.prototype.trueLength= function(){
  43. var list= [], ctr = 0, array = this;
  44.  
  45. for(var i in array) (function(arr) {
  46.  
  47. if(array.hasOwnProperty(i)){
  48. list.push(arr);
  49. ctr++;
  50. };
  51.  
  52.  
  53. }(array[i]));
  54.  
  55. return {length: ctr, "list": list}
  56. }
  57.  
  58. var myArray2 =[];
  59. myArray2[10]='44';
  60. myArray2[55]='55';
  61.  
  62. // list not undefined
  63. myArray2.trueLength().list // ["44", "55"]
  64.  
  65. // not undefined length list
  66. myArray2.trueLength().length // 2
  67.  
  68. function getArrayPropertyCount(arr) {
  69. var count = 0;
  70. for (var i = 0, len = arr.length; i < len; ++i) {
  71. if (i in arr) {
  72. ++count;
  73. }
  74. }
  75. return count;
  76. }
Add Comment
Please, Sign In to add comment