0

I have lightning controller in were I want to filter a list based on a datefield. I tried the following but end up with an empty list:

My list has 2 records:

serverList[0].Startdatum__c = 1-12-2018
serverList[1].Startdatum__c = 30-7-2018

startdate = 02-08-2018


var serverListFilteredbyDate = serverList.filter(function(item) { return item[(serverList[i].Startdatum__c >= startDate)]});   
1
  • 1
    Should not it be like - var serverListFilteredbyDate = serverList.filter(function(item) {return item.Startdatum__c >= startDate;}); ?
    – S..
    Commented Jun 27, 2018 at 13:33

1 Answer 1

3

As written, you're trying to subtract numbers (02-08-2018 = -2024). Instead, you need to make sure you're using Date objects:

startdate = new Date("02-08-2018")

I believe you'll also need to do this for the values in the field as well.

As N.B. commented while I was writing, you use "item" (the variable) to do the filtering. You could actually write it more like this:

startdate = new Date("02-08-2018")
var serverListFilteredbyDate = serverList.filter(item => new Date(item.Startdatum__c) >= startDate);   
3
  • I tried your suggestion but I end up with an "item is not defined"
    – Thomas
    Commented Jun 27, 2018 at 14:04
  • I just wrote similar code and tested it just now, it works for me.
    – sfdcfox
    Commented Jun 27, 2018 at 14:10
  • When I replace item with serverList it works. Thanks!
    – Thomas
    Commented Jun 27, 2018 at 14:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .