0

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
}];      */                

         

3
  • 6
    That's nice. Good luck with that. Did you have a programming question? Commented Jul 27, 2015 at 20:55
  • but you are not really trying to do it! BTW it is i < data.length, with i > data.length you will not execute anything (if idx should be i, if not you are so far) Commented Jul 27, 2015 at 20:55
  • give Array.filter a try. Commented Jul 27, 2015 at 20:58

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0
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]});
}

2 Comments

well if think that i>data.length will not even execute the first iteration
You're totally right, I copy pasted the code and didn't notice, I've fixed it. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.