簡體   English   中英

如何過濾數組中的對象數組

[英]how to filter an array of objects inside an array

我有一個內部有“句點”數組的 json,我已經讓過濾器代碼按我想要的方式工作,但是我無法訪問 json 中的屬性

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] } ] } ]

過濾器

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 ) ;

我無法訪問“periodos”數組

在這里,您嘗試在沒有索引的情況下訪問數組,如下所示。

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

periodos是一個數組,因此要訪問它的對象屬性,您需要提供類似這樣的索引。

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

注意 - 在這里,索引將根據您的業務要求,可以是 0,1,2...n。 考慮到 n 是數組的長度/最后一個索引。

希望它會有所幫助。

你可以使用這樣的東西

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM