Home > front end >  how to filter an array of objects inside an array
how to filter an array of objects inside an array

Time:06-22

I have a json that has the "periods" array inside, I already have the filter code working the way I want, but I can't access the properties inside the json

the json this.transporte

[ { "id": 1, "emp_id": 1, "nome": "Retirada na Loja", "tipo": "RETIRA", "subtipo": null, "latLng": [-25.45264, -49.26653], "vFreteMin": 0, "vFreteGratis": null, "periodos": [ { "id": 8, "transporte_id": 1, "ativo": 1, "periodo": "Comercial (das 8h às 19h)", "corte": "16:00", "data": null, "week": [0, 1, 1, 1, 1, 1, 1] }, { "id": 16, "transporte_id": 1, "ativo": 1, "periodo": "Domingos ou Feriado (Das 9h as 14h)", "corte": "12:00", "data": null, "week": [1, 0, 0, 0, 0, 0, 0] } ] }, { "id": 2, "emp_id": 1, "nome": "Frota Própria", "tipo": "FROTA", "subtipo": null, "latLng": [-25.4522, -49.267], "vFreteMin": 0, "vFreteGratis": 80, "periodos": [ { "id": 4, "transporte_id": 2, "ativo": 1, "periodo": "COMERCIAL (9h as 19h)", "corte": "16:00", "data": "2022-03-24", "week": [0, 1, 1, 1, 1, 1, 1] }, { "id": 17, "transporte_id": 2, "ativo": 1, "periodo": "Domingos ou Feriados (Das 9h as 14h)", "corte": "09:30", "data": null, "week": [1, 0, 0, 0, 0, 0, 0] } ] }, { "id": 20, "emp_id": 1, "nome": "Correios (PAC)", "tipo": "TRANSP", "subtipo": null, "latLng": [-25.4522, -49.267], "vFreteMin": null, "vFreteGratis": null, "periodos": [ { "id": 18, "transporte_id": 20, "ativo": 1, "periodo": "Comercial (Das 8h as 18h)", "corte": "17:30", "data": null, "week": [0, 1, 1, 1, 1, 1, 1] } ] } ]

the filter

filterPeriodos() {
let object = this.formGeral.value.data;
let jsDate = new Date(object.singleDate?.jsDate);
jsDate.setUTCHours(23,59,59,999);
this.dataFormat=jsDate
const d = new Date(jsDate );
if (this.storeS.layout.emp.id === 1) {
    if(this.formGeral.value.entregaBool){
        return this.transporte.filter( transpId =>   transpId.periodos.ativo === (1) && transpId.periodos.transporte_id === (this.metodoId) && transpId.periodos.week[d.getDay()] === 1 ) ;
    }
}
return this.transporte;
}


this.transporte.filter( transpId =>   transpId.periodos.ativo === (1) && transpId.periodos.transporte_id === (this.metodoId) && transpId.periodos.week[d.getDay()] === 1 ) ;

I'm not able to access the "periodos" array

CodePudding user response:

Here, you are trying to access the array without it's index as given below.

this.transporte.filter( transpId => transpId.periodos.ativo === (1) && transpId.periodos.transporte_id === (this.metodoId) && transpId.periodos.week[d.getDay()] === 1 );

periodos is an array so to access it's object's properties you need to provide index something like this .

this.transporte.filter( transpId => transpId.periodos[index].ativo === (1) && transpId.periodos[index].transporte_id === (this.metodoId) && transpId.periodos[index].week[d.getDay()] === 1 );

Note - Here, index will be as per your business requirement and can be like 0,1,2...n. considering n is the length/ last index of the array.

Hope it will help.

CodePudding user response:

you can use something like this

this.transporte.filter(transpId =>transpId.periodos?.filter((p)=>p.ativo === (1) && p.transporte_id === (this.metodoId) && p.week[d.getDay()] === 1 ));
  • Related