jeudi 1 octobre 2020

Array keeps pushing the same object in loop or just pushes a single object [duplicate]

Hi I have been trying to push the filterCategory and selected filter when clicked on a check box. These are passed as filterName and filterValue respectively. The on change function just pushes the last selected value into the array. I even tried creating an empty object inside the for loop but it didn't work. Can someone help?

getSelectedFilter(filterName, filterValue) {
      const obj = {fieldName: filterName, values: [filterValue]}
      const filterArray = [];
        const checked = document.querySelectorAll('input[type=checkbox]:checked')
        checked.forEach(() => {
          filterArray.push(obj)
        })
        let requestObject ={
          filters: filterArray,
        console.log(requestObject)
      }



Aucun commentaire:

Enregistrer un commentaire