3

I'm trying to filter the date which where the FROM and TO will display the data,

but when the FROM and TO are the same it doesn't display the current date selected.

Example FROM: 08-01-2019 00:00:00 | To: 08-29-2019 13:00:00 it doesn't display the data 2019-08-28 12:36:01, but when I try to set the FROM: 06-14-2019 00:00:00 | TO: 08-29-2019 13:00:00 it displays all the data

templog.component.ts

columnDefs: any = [
    { headerName: 'Date Time', field: 'dateandtime' },
    { headerName: 'Location', field: 'sensor' },
    { headerName: 'Temperature', field: 'temperature' },
    { headerName: 'Humidity', field: 'humidity' }
  ];

submitForm(): void {
    this.rowData = record.default.records;

    const dateStart = format(this.validateForm.value.datePickerStart, 'YYYY-MM-DDTHH:mm:ss');
    const dateEnd = format(this.validateForm.value.datePickerEnd, 'YYYY-MM-DDTHH:mm:ss');
    
    for(let i = 0;i < this.rowData.length; i++){
      if (isBefore(this.rowData[i].dateandtime, dateStart) || 
        isAfter(this.rowData[i].dateandtime, dateEnd )) {  
          this.rowData = this.rowData.splice(i, 0);
      }
    }    
  }

templog.json

{
    "records": [
        {
            "dateandtime": "2018-06-14 01:38:02",
            "sensor": "Sewing Line1",
            "temperature": "25.8",
            "humidity": "99.9"
        },
        {
            "dateandtime": "2018-06-14 01:36:01",
            "sensor": "Sewing Line1",
            "temperature": "25.8",
            "humidity": "99.9"
        },
        {
            "dateandtime": "2018-06-14 01:36:01",
            "sensor": "Heat Seal Area",
            "temperature": "25.9",
            "humidity": "99.9"
        },
        {
            "dateandtime": "2019-08-28 12:36:01",
            "sensor": "Heat Seal Area",
            "temperature": "25.9",
            "humidity": "99.9"
        }
    ]
}
5
  • 2
    When you need to remove multiple items in an array, you need to do it from the last index. If you do it from the front, once you removed an item, the next item will replace the current index. Or you can do i-- after removal. Commented Aug 29, 2019 at 12:56
  • Or simply use filter Commented Aug 29, 2019 at 13:14
  • @Icycool thank you sir. but I already tried on it. still it doesn't work. Commented Aug 29, 2019 at 13:37
  • @Random where I will apply the "filter"? Commented Aug 29, 2019 at 13:41
  • @ABC please see my answer Commented Aug 29, 2019 at 14:01

2 Answers 2

2

Your for loops can be simplified with a filter

export interface MyRecord {
    dateandtime: string,
    sensor: string,
    temperature: string,
    humidity: string
}

public rowData: MyRecord[] = [];

submitForm(): void {
  this.rowData = record.default.records;

  const dateStart = format(this.validateForm.value.datePickerStart, 'YYYY-MM-DDTHH:mm:ss');
  const dateEnd = format(this.validateForm.value.datePickerEnd, 'YYYY-MM-DDTHH:mm:ss');

  this.rowData = this.rowData.filter((data: MyRecord) => {
    // keep data which are not out of date-bound
    return !isBefore(data.dateandtime, dateStart) && !isAfter(data.dateandtime, dateEnd);
  });  
}
Sign up to request clarification or add additional context in comments.

8 Comments

Parameter 'data' implicitly has an 'any' type.ts(7006) - should I set the data as any? or (data: { dateandtime: string | number | Date; })?
is rowData typed ? If so, data has the same type (removing the array). If rowData: MySuperType[], then data: MySuperType.
because I set the rowData: any = []; then this.rowData = record.default.records; where it will get the data inside the templog.json.
(data: this.rowData) it doesn't work. it should get the data of rowData
@ABC ok, I'll add how to use types too since you seem not to fully understand how it works
|
0

Resolved

Earlier you were saying FROM: 08-29-2019 00:00:00 To: 08-29-2019 13:00:00 it doesn't display the data 2019-08-28 12:36:01 which is true. Because 2019-08-28 12:36:01 is not between those range.

And now since you have updated the question, if you want to filter mentioned date please follow below simple JavaScript solution, you can convert it to angular.

var data=[
    {
        "dateandtime": "2018-06-14 01:38:02",
        "sensor": "Sewing Line1",
        "temperature": "25.8",
        "humidity": "99.9"
    },
    {
        "dateandtime": "2018-06-14 01:36:01",
        "sensor": "Sewing Line1",
        "temperature": "25.8",
        "humidity": "99.9"
    },
    {
        "dateandtime": "2018-06-14 01:36:01",
        "sensor": "Heat Seal Area",
        "temperature": "25.9",
        "humidity": "99.9"
    },
    {
        "dateandtime": "2019-08-28 12:36:01",
        "sensor": "Heat Seal Area",
        "temperature": "25.9",
        "humidity": "99.9"
    }
]
data.filter(function(x){ 
                    if(new Date(x.dateandtime)> new Date('06-12-2019 00:00:00') 
                    && new Date(x.dateandtime)< new Date('08-28-2019 14:00:00')){ return x }
      })

5 Comments

indeed, the error comes from the test, good catch. But please do not use function(x), use arrow functions (x =>) instead...
Sir I'm using isBefore and isAfter
ok sir. I already edit my question FROM: 08-01-2019 TO: 08-29-2019. sorry for mistake. but when i try it to run it doesn't show on my code
@ABC I couldn't see you mentioned anything about moment.js. I belive isBefore and isAfter are moment.js functions. But anyways try testing with inbetween date range and see. It should work.
but it works when the range is june 14, 2018 - august 29,2019 the will display

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.