Guest User

Untitled

a guest
Jan 16th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. var arr = ['ab','pq','mn','ab','mn','ab']
  2.  
  3. arr['ab'] = 3
  4. arr['pq'] = 1
  5. arr['mn'] = 2
  6.  
  7. $.each(arr, function (index, value) {
  8. if (value)
  9. arr[value] = (resultSummary[value]) ? arr[value] + 1 : 1;
  10. });
  11.  
  12. console.log(arr.join(','));
  13.  
  14. var occurrences = { };
  15. for (var i = 0, j = arr.length; i < j; i++) {
  16. occurrences[arr[i]] = (occurrences[arr[i]] || 0) + 1;
  17. }
  18.  
  19. console.log(occurrences); // {ab: 3, pq: 1, mn: 2}
  20. console.log(occurrences['mn']); // 2
  21.  
  22. var occurrences = arr.reduce(function(obj, item) {
  23. obj[item] = (obj[item] || 0) + 1;
  24. return obj;
  25. }, {});
  26.  
  27. console.log(occurrences); // {ab: 3, pq: 1, mn: 2}
  28. console.log(occurrences['mn']); // 2
  29.  
  30. var a = [true, false, false, false];
  31. a.filter(function(value){
  32. return value === false;
  33. }).length
  34.  
  35. arr.count('ab');
  36.  
  37. var result = {};
  38. function count(input){
  39. var tmp = 0;
  40. if(result.hasOwnProperty(input)){
  41. tmp = result[input];
  42. result[input]=tmp+1;
  43. }else{
  44. result[input]=1;
  45. }
  46. }
  47.  
  48. var report = {};
  49.  
  50. arr.forEach(function(el){
  51. report[el] = report[el] + 1 || 1;
  52. });
  53.  
  54. var report = {};
  55.  
  56. $.each(arr,function(i,el){
  57. report[el] = report[el] + 1 || 1;
  58. });
  59.  
  60. console.log( report );
  61.  
  62. var arr = ['ab','pq','mn','ab','mn','ab']
  63.  
  64. jQuery.grep(arr, function(a){
  65. return a == 'ab'
  66. }).length // 3
  67.  
  68. ['ab','pq','mn','ab','mn','ab'].filter(function(value){
  69. return value == 'ab'
  70. }).length // 3
  71.  
  72. $.each(arr, function(index, value) {
  73. if (!resultSummary[value]){
  74. resultSummary[value] = 0;
  75. }
  76. resultSummary[value] += 1;
  77. });
  78.  
  79. var arr = ['ab','pq','mn','ab','mn','ab']
  80.  
  81.  
  82.  
  83. function getCount(arr,val)
  84. {
  85. var ob={};
  86. var len=arr.length;
  87. for(var k=0;k<len;k++)
  88. {
  89. if(ob.hasOwnProperty(arr[k]))
  90. {
  91. ob[arr[k]]++;
  92. continue;
  93. }
  94. ob[arr[k]]=1;
  95. }
  96. return ob[val];
  97. }
  98.  
  99.  
  100.  
  101. //run test
  102. alert(getCount(arr,'ab'));//3
  103.  
  104. var arr = ['ab','pq','mn','ab','mn','ab'];
  105. var result = { };
  106. for(i=0;i<arr.length;++i)
  107. {
  108. if(!result[arr[i]])
  109. result[arr[i]]=0;
  110. ++result[arr[i]];
  111. }
  112.  
  113. for (var i in result){
  114. console.log(i+":"+result[i]);
  115. }
Add Comment
Please, Sign In to add comment