Search code examples
arraysfiltercoffeescriptrecords

Filtering an array of records in coffeescript to return all records with age > 50


Given this sort of data set

records = [
  {id: 1, name: "John Smith", age: 49},
  {id: 2, name: "Jane Brown", age: 45},
  {id: 3, name: "Tim Jones",  age: 60},
  {id: 4, name: "Blake Owen", age: 78}    
]

How do I filter the records to return a reduced array that is just over the age of 50.

Say the return array is

over50s = // something

I've looked at lots of similar code but it's just quite coming together for me.

Thanks for your help!


Solution

  • How about this sample script? The result over50s is retrieved using filter().

    Sample script :

    records = [
      {id: 1, name: "John Smith", age: 49},
      {id: 2, name: "Jane Brown", age: 45},
      {id: 3, name: "Tim Jones",  age: 60},
      {id: 4, name: "Blake Owen", age: 78}
    ]
    over50s = records.filter (e) -> e.age >= 50
    

    Result :

    [
        { id: 3, name: 'Tim Jones',  age: 60 },
        { id: 4, name: 'Blake Owen', age: 78 }
    ]
    

    If I misunderstand your question, please tell me. I would like to modify.