Home > Blockchain >  I want to filter an array but the filter function doesn't respond well
I want to filter an array but the filter function doesn't respond well

Time:05-12

I want to filter my array with several elements but it turns out the .filter does not work, the value that filterInDeep returns works but the .filter() of applyfilter() does not take into account the return:

const maquettesListRef = [
    {
        "idMaquette": 1,
        "codeMaquette": "gsgrcs001",
        
        "version": {
            "idVersion": 3,
            "versionG2s": "202110"
        },
        "context": {
            "idContext": 1,
            "libelle": "TP"
        },
        "application": {
            "idApplication": 1,
            "libelle": "nova",

        },
        "baseTemplate": {
            "idBaseTemplate": 1,
            "code": "base_template1",

            "gabarit": {
                "idGabarit": 1,
                "code": "gabarit1",

            }
        },
        "secteur": {
            "idSecteur": 9,
            "libelle": "Prévoyance collective",
            "lstEnfant": []
        },
        "provenance": ""
    },
    {
        "idMaquette": 3,
        "codeMaquette": "gsgrcs002",
        "version": {
            "idVersion": 2,
            "versionG2s": "202107"
        },
        "context": {
            "idContext": 2,
            "libelle": "BATCH"
        },
        "application": {
            "idApplication": 2,

        },
        "baseTemplate": {
            "idBaseTemplate": 1,
            "code": "base_template1",

            "gabarit": {
                "idGabarit": 1,
                "code": "gabarit1",

            }
        },
        "secteur": {
            "idSecteur": 8,
        },
        "provenance": ""
    },
    {
        "idMaquette": 4,
        "codeMaquette": "gsgrcs003",
        "version": {
            "idVersion": 2,
            "versionG2s": "202107"
        },
        "context": {
            "idContext": 3,
            "libelle": "TP/BATCH"
        },
        "application": {
            "idApplication": 2,
        },
        "baseTemplate": {
            "idBaseTemplate": 1,
            "code": "base_template1",

            "gabarit": {
                "idGabarit": 1,
                "code": "gabarit1",

            }
        },
        "secteur": {
            "idSecteur": 8,
            "libelle": "souscription Vie Individuelle",
            "lstEnfant": []
        },
        "provenance": ""
    }
]
const filter = {
    "codeMaquette": "g",
    "version": {
        "idVersion": 1
    }
}

First call to applyFilter() function

my filtreEnProfondeur function works it returns me the value true or false at the right time, but at the time of the callback this one no longer responds and does not return the expected array.

  filterInDeep(object:any,filtre:any){
    return Object.keys(filtre).every((c) =>{
      switch (typeof filtre[c]){
        case "string":
          return (object[c].includes(filtre[c]))
        case "number":
          return (object[c] == filtre[c])
        case "object":
          this.filterInDeep(object[c],filtre[c])
      }
    })
  }


  applyFilter(filter: any) {
  console.log(this.maquettesListRef)
    console.log(filter)
    console.log(this.maquettesListRef.filter((maquette) =>{
      this.filtreEnProfondeur(maquette,filter)
    }))
}

Actual return

[]

Expected return

[
    {
        "idMaquette": 1,
        "codeMaquette": "gsgrcs001",
        
        "version": {
            "idVersion": 3,
            "versionG2s": "202110"
        },
        "context": {
            "idContext": 1,
            "libelle": "TP"
        },
        "application": {
            "idApplication": 1,
            "libelle": "nova",

        },
        "baseTemplate": {
            "idBaseTemplate": 1,
            "code": "base_template1",

            "gabarit": {
                "idGabarit": 1,
                "code": "gabarit1",

            }
        },
        "secteur": {
            "idSecteur": 9,
            "libelle": "Prévoyance collective",
            "lstEnfant": []
        },
        "provenance": ""
    }
]

CodePudding user response:

It doesn't work for you since you need to return bool from filter in each call. and since you use { } you don't return anything. Try use return:

applyFilter(filter: any) {
  console.log(this.maquettesListRef)
  console.log(filter)
  console.log(
    this.maquettesListRef.filter((maquette) => {
      return this.filtreEnProfondeur(maquette,filter)
    })
  )

here's a simplified example that demonstrate your problem and the solution for it:

const arr = [1, 2, 3, 4, 5, 6];

console.log(arr.filter(v => {
  v % 2 == 0
}))
console.log(arr.filter(v => {
  return v % 2 == 0
}))

  • Related