-2

I've been trying for two hours to figure this out and have found some articles related to my question but I can't seem to make the suggestions work in my situation. So I hope you can help.

I have an array of objects called personArray with 50,000 entries, here are two sample entries:

[
  { sex: "Male", 
    lastName: "DIXON", 
    firstName: "Esteban", 
    age: 51, 
    isSingle: true
  }, 
  {
    sex: "Female", 
    lastName: "BULLOCK", 
    firstName: "Daniela", 
    age: 58, 
    isSingle: false
  } 
]

I want to be able to get useful demographic information out of that data. For instance, I would like to know how many Male between the age of 20 and 24 isSingle. How do I do that?

As mentioned, I have read other related answers but, being rather new at this, have been unable to translate them to my particular situation. I'm not looking to find a single object, or a single value of a property of a single object. I'm looking to find how many entries out of the 50 000 fit the parameters mentioned above.

TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
snowfrogdev
  • 5,963
  • 3
  • 31
  • 58
  • Write a `for` loop that increments a counter whenever the conditions are met. – Barmar Aug 12 '16 at 18:51
  • Possible duplicate of [How do I check if an array includes an object in JavaScript?](http://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-an-object-in-javascript) – Heretic Monkey Aug 12 '16 at 19:01
  • @MikeMcCaughan This doesn't seem similar to that at all. He wants to count all the objects that match some criteria, not search for a particular object. – Barmar Aug 12 '16 at 19:10
  • Is it that much of a stretch from finding one object that matches some criteria to finding all objects that match some criteria? – Heretic Monkey Aug 12 '16 at 19:16
  • @Barmar Thanks, you get it. – snowfrogdev Aug 12 '16 at 19:17
  • @MikeMcCaughan For many people, probably not. But, as I have stated in my question, being dumber than most, I wasn't able to figure it out. It may seem easy to all you experienced programmers to take a similar concept and know how to apply it to a slightly different situation, but I literally have 3 weeks of experience with programming. I'm reading alot, learning alot and I'm forging ahead with my project. When I hit a wall, I do some reasearch, I try things out, and when that fails I post a question here. Isn't that what this site is for? – snowfrogdev Aug 12 '16 at 19:30

2 Answers2

2

Functional approach:

var count = input.filter(function(item) {
    return item.sex == 'Male' && item.age > 19 && item.age < 25 && item.isSingle;
}).length;

Imperative approach:

var count = 0;
input.forEach(function(item) {
    if (item.sex == 'Male' && item.age > 19 && item.age < 25 && item.isSingle) count++;
});
TimoStaudinger
  • 41,396
  • 16
  • 88
  • 94
1

Use Array.prototype.filter. You can find the detailed info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

var a = [{
  sex: "Male",
  lastName: "DIXON",
  firstName: "Esteban",
  age: 51,
  isSingle: true
}, {
  sex: "Male",
  lastName: "DOE",
  firstName: "John",
  age: 21,
  isSingle: true
}, {
  sex: "Female",
  lastName: "BULLOCK",
  firstName: "Daniela",
  age: 58,
  isSingle: false
}];

a = a.filter(function(el) {
  return el.sex === "Male" && el.age > 20 && el.age < 24 && el.isSingle === true;
});

console.log(a);
/* 
[ { sex: 'Male',
    lastName: 'DOE',
    firstName: 'John',
    age: 21,
    isSingle: true } ]
*/
smddzcy
  • 443
  • 4
  • 19
  • 1
    I don't want it to return the entries that fit my criteria, I want to know how many entries actually fit my criteria. I want a number. Also, would your suggested method destroy the original array data? – snowfrogdev Aug 12 '16 at 19:21