-2

In the function, I get the key endDate and value of 03-10-2000 and 01-10-2000. I want to filter by those two conditions given. So I check if the end date is between two date ranges. So the end result would be as below as only that object is in between two date ranges.

date= [{endDate:'03-10-2000', customerName:'Jay', startDate:'17-12-2000'}].

Update

function(_key, _startDate, _endDate){
     //here key ='endDate'.. so I need to make use of the key..
   this.data = data.filter(item => item._key > '01-10-2000' && item._key < '03-10-2000')

}

update 2 this is what I can think of

cost emptyArray = [];
Object.keys(this.data).forEach(function(key) { 
    if(key == this.key && this.data[key] >= this.startDate && 
       this.data[key]>=this.endDate)
    {
       emptyArray.push(this.data[key])
    }
});

Example data

date = [
    {endDate:'12-12-2000', customerName:'Joe', startDate:'15-12-2000'},
    {endDate:'03-10-2000', customerName:'Jay', startDate:'17-12-2000'},
    {endDate:'02-12-2000', customerName:'Kim', startDate:'11-12-2000'} 
];

dumb11
  • 129
  • 1
  • 9
  • 2
    I'd try [Array.prototype.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) – Jordi Nebot May 26 '20 at 08:25
  • that's what I thought first. how do you check if the enddate equals the key of the object array and then do filter? – dumb11 May 26 '20 at 08:27
  • please add the meaning of your date string. – Nina Scholz May 26 '20 at 08:31
  • date is not a string but a date format – dumb11 May 26 '20 at 08:38
  • yes. and? `dd-mm-jjjj` or `mm-dd-jjj`? – Nina Scholz May 26 '20 at 08:39
  • yeah such as ```moment(this.date).format("DD-MM-YYYY")``` so I can think of like ```Object.Keys or something then item[this.key] > 03-12-2020``` like that? – dumb11 May 26 '20 at 08:40
  • now, it's more confusing. where is `this` coming from? why do you have a variable key and fixed values for checking. what is the reason to check if `key` is equal to `this.key`? why do you have a wrong comparison (the second with `>= this.endDate`)? what do you want by iterating the keys of the object? please add what you have, what is a variable content and what you like to get. – Nina Scholz May 26 '20 at 08:49
  • Then your question is not about filtering arrays but comparing dates... – Jordi Nebot May 26 '20 at 09:34

2 Answers2

0
date.filter(item => item.endDate > '01-10-2000' && item.endDate  < '03-10-2000')

try this

Anh Tuan
  • 1,113
  • 5
  • 12
  • I know this works.. but before that I need to check the key I get the key from ```this.key``` then. date.filter(item => item.this.key> '01-10-2000' && item.this.key< '03-10-2000') such way? – dumb11 May 26 '20 at 08:32
  • how about item?.[this.key] have you try it and are you sure this.key have default value – Anh Tuan May 26 '20 at 08:35
0

The problem to use the 'filter' method here (as you suggested in your title) is that the dates are in a format not very good for comparisons. I suggest to create a date comparison method appropriate for this format:

function compareDate(date1, date2) {
    if (date1 === date2) return 0;
    const validFormatDate1 = date1.split('-').reverse().join('');
    const validFormatDate2 = date2.split('-').reverse().join('');
    return date1>date2 ? 1 : -1;
}

In this way you can use it into your filter:

function isValidDate(date, min, max) {
    return compareDate(min, date) === -1 && compareDate(date, max) === -1;
}
const filteredDate = date.filter(({endDate}) => isValidDate(endDate, minDate, maxDate));
Raffaele
  • 737
  • 7
  • 21