Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var arr = ['ab','pq','mn','ab','mn','ab']
- arr['ab'] = 3
- arr['pq'] = 1
- arr['mn'] = 2
- $.each(arr, function (index, value) {
- if (value)
- arr[value] = (resultSummary[value]) ? arr[value] + 1 : 1;
- });
- console.log(arr.join(','));
- var occurrences = { };
- for (var i = 0, j = arr.length; i < j; i++) {
- occurrences[arr[i]] = (occurrences[arr[i]] || 0) + 1;
- }
- console.log(occurrences); // {ab: 3, pq: 1, mn: 2}
- console.log(occurrences['mn']); // 2
- var occurrences = arr.reduce(function(obj, item) {
- obj[item] = (obj[item] || 0) + 1;
- return obj;
- }, {});
- console.log(occurrences); // {ab: 3, pq: 1, mn: 2}
- console.log(occurrences['mn']); // 2
- var a = [true, false, false, false];
- a.filter(function(value){
- return value === false;
- }).length
- arr.count('ab');
- var result = {};
- function count(input){
- var tmp = 0;
- if(result.hasOwnProperty(input)){
- tmp = result[input];
- result[input]=tmp+1;
- }else{
- result[input]=1;
- }
- }
- var report = {};
- arr.forEach(function(el){
- report[el] = report[el] + 1 || 1;
- });
- var report = {};
- $.each(arr,function(i,el){
- report[el] = report[el] + 1 || 1;
- });
- console.log( report );
- var arr = ['ab','pq','mn','ab','mn','ab']
- jQuery.grep(arr, function(a){
- return a == 'ab'
- }).length // 3
- ['ab','pq','mn','ab','mn','ab'].filter(function(value){
- return value == 'ab'
- }).length // 3
- $.each(arr, function(index, value) {
- if (!resultSummary[value]){
- resultSummary[value] = 0;
- }
- resultSummary[value] += 1;
- });
- var arr = ['ab','pq','mn','ab','mn','ab']
- function getCount(arr,val)
- {
- var ob={};
- var len=arr.length;
- for(var k=0;k<len;k++)
- {
- if(ob.hasOwnProperty(arr[k]))
- {
- ob[arr[k]]++;
- continue;
- }
- ob[arr[k]]=1;
- }
- return ob[val];
- }
- //run test
- alert(getCount(arr,'ab'));//3
- var arr = ['ab','pq','mn','ab','mn','ab'];
- var result = { };
- for(i=0;i<arr.length;++i)
- {
- if(!result[arr[i]])
- result[arr[i]]=0;
- ++result[arr[i]];
- }
- for (var i in result){
- console.log(i+":"+result[i]);
- }
Add Comment
Please, Sign In to add comment