Red Devil
Red Devil

Reputation: 176

Counting occurrences of Javascript array

I'm trying to count the number of occurrences in one array of objects and return another array with that occurrences.

var data = [
    {
        absCost: 0,
        absEndDate: "2014-04-24T00:00:00+01:00",
        absId: "#",
        absMins: 3152,
        absStartDate: "2015-04-08T00:00:00+01:00",
        absType: "Annual Leave",
        absenceReason: "",
        authorise: "Undecided",
        department: "St Catherine's Ward",
        empName: "Alison Lane",
        empNo: " 06547",
        empid: 575,
        status: "",
        year: 2014
    },{
        absCost: 0,
        absEndDate: "2015-04-24T00:00:00+01:00",
        absId: "#",
        absMins: 3152,
        absStartDate: "2015-04-08T00:00:00+01:00",
        absType: "Annual Leave",
        absenceReason: "",
        authorise: "Undecided",
        department: "St Catherine's Ward",
        empName: "Alison Lane",
        empNo: " 06547",
        empid: 575,
        status: "",
        year: 2015
    },
    {
        absCost: 0,
        absEndDate: "2015-04-24T00:00:00+01:00",
        absId: "#",
        absMins: 3152,
        absStartDate: "2015-04-08T00:00:00+01:00",
        absType: "Annual Leave",
        absenceReason: "",
        authorise: "Undecided",
        department: "St Catherine's Ward",
        empName: "Alison Lane",
        empNo: " 06547",
        empid: 575,
        status: "",
        year: 2015
    }];


var finalArray = [];


for (var idx=0; i>data.length; i++){
  finalArray.push({
    year: data[i].year,
    frequency: //number of occurrences of the Year
  });
}


//Hi Would like to get this result:

/*
finalArray=[{
   year: 2014,
   frequency: 1
},{
   year: 2015,
   frequency: 2
}];      */                

         

Upvotes: 0

Views: 133

Answers (3)

eddyjs
eddyjs

Reputation: 1280

var final = {};
for (var i=0; i<data.length; i++){
  final[data[i].year] = final[data[i].year] || 0;
  final[data[i].year]++;
}

This will give you an object with the years as keys and the number of occurrences as the values, and create a key value pair if it doesn't exist yet.

if you'd like, you can then create a new array with the properties you want:

var finalArray = [];
for (var key in final) {
    finalArray.push({year: key, frequency: final[key]});
}

Upvotes: 0

Francesco
Francesco

Reputation: 1413

You could have tried a bit more, anyway this is very easy approach:

var years = [];

for (var i=0; i<data.length; i++){
  years.push(data[i].year);
}
years.sort();

var finalArray = [];
var occurence = 1;
for (var i=0; i<=years.length-1; i++){
    console.log(years[i])
    if (years[i] === years[i+1]) {
        occurence += 1;
    } else {
        finalArray.push({year: years[i], frequency: occurence});
        occurence = 1;
    }
}

DEMO

Upvotes: 1

aleju
aleju

Reputation: 2386

You could use an associative array in combination with filter() to achieve that goal:

var dict = {};
for (var i=0; i<data.length; i++){
    var year = data[i].year;
    if (!(year in dict)) {
        dict[year] = data.filter(function(row) {
            return row.year == data[i].year
        }).length;
    }
}

jsfiddle

Upvotes: 1

Related Questions