1

I'm fetching data from SharePoint using REST, and everything works just fine, except that I would like to count the times the same item appears.

This is the jQuery:

 var url = "https:xxxxxxxx/_vti_bin/ListData.svc/RMSD_Tasks?$orderby=TypeOfIssueValue asc,StatusValue desc&$filter=StatusValue ne 'Completed'&groupby=TypeOfIssueValue/StatusValue";
var lastIssue = '';

$.getJSON(url, function (data) {

$('#totalCounter').text(data.d.results.length);


for (var i = 0; i < data.d.results.length; i++) {

    var dateReceived = data.d.results[i].DateReceived;
    dateReceived = new Date(parseInt(dateReceived.replace("/Date(", "").replace(")/", ""), 10)).toLocaleString('en-US', {
        year: 'numeric',
        month: 'numeric',
        day: '2-digit'
    });
    var issue = data.d.results[i].TypeOfIssueValue;
        console.log(data.d.results[i].TypeOfIssueValue);

    if (issue != lastIssue) {

        lastIssue = issue;

        $('#myDataList').append('<a href="#" class="list-group-item">' + issue + '<span class="badge">' + issue.length + '</span></a>');

    }

}


});

I need to count how many time a specific TypeOfIssueValue appears. When I see the console it shows exactly what I would like to add to me info:

enter image description here

I just added a issue.length in the badge were I want to insert the number for the sake of just having something there, but I know it won't show what I want. Thanks!

Community
  • 1
  • 1
cubanGuy
  • 1,226
  • 3
  • 26
  • 48

3 Answers3

2

var data = {
    d: {
        results: [
            { TypeOfIssueValue: '456' },
            { TypeOfIssueValue: '123' },
            { TypeOfIssueValue: '789' },
            { TypeOfIssueValue: '123' }
        ]
    }
};

var filteredItems = data.d.results.filter(function(item){
   return item.TypeOfIssueValue == '123'; 
});
var count = filteredItems.length;

document.getElementById("output").innerHTML = "Number of items with value '123': " + count;
<div id="output"/>
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
1

You could first map the TypeOfIssueValue values to a new array and then count each occurence based on this answer.

The code would be :

var a = data.d.results.map(function(issue) {
    return issue.TypeOfIssueValue
});
result = {};
for (i = 0; i < a.length; ++i) {
    if (!result[a[i]])
        result[a[i]] = 0;
    ++result[a[i]];
}

The result will be an object with property being type of issue and value being the count of each.

Let me know if this makes sense.

Community
  • 1
  • 1
thispatchofsky
  • 854
  • 6
  • 15
0

Thanks @srinivas. I accepted your response, although I made some modifications, just in case they are useful to someone else.

I added a class to the span badge and added a new array to push the issues:

 issuesArray.push(data.d.results[i].TypeOfIssueValue);

$('#myDataList').append('<a href="#" class="list-group-item dataLI">' + issue + '<span class="badge badgeSpan"></span></a>');

Then I addded a done() to run after the getJSON:

.done(

 function(){ var resultado = foo(issuesArray)[1]; 
 console.log(resultado);
 var badges = $('.badgeSpan');
 for (var j = 0; j < resultado.length; j++){
 badges[j].innerHTML = resultado[j];

 }

 });

Last I made a small modificfation to the function foo() that you provided:

testArray = [];
function foo(arr) {

var a = [], b = [], prev;

for ( var i = 0; i < arr.length; i++ ) {
    if ( arr[i] !== prev ) {
        a.push(arr[i]);
        b.push(1);
    } else {
        b[b.length-1]++;
    }
    prev = arr[i];
}
testArray.push(a,b)
return  testArray;

}

This maybe a very unorthodox solution, but it worked for me. Thanks again.

cubanGuy
  • 1,226
  • 3
  • 26
  • 48