Home > Software engineering >  JavaScript Filter Loop Not Looping Through All Item In Array
JavaScript Filter Loop Not Looping Through All Item In Array

Time:12-28

In the following code I am trying to filter the data. However, I see in the algorithm that it loops through the first item in the userPassedFilter array and returns the filtered data but doesn't loop again to check the second item in the userPassedFilter array. I know this is happening because I call return true before it iterates through the entire userPassedFilter array. How can I assure that in the algorythm that it loops through all the items in the userPassedFilter before returning the final result?

**User Selected Filter**
let userPassedFilter = [{"phase":1},{"phase":2}]
**Data**
let dataList = [
{"voltage":35, "phase":2},
{"voltage":12, "phase":2},
{"voltage":12, "phase":3},
{"voltage":35, "phase":1}
]
**Filter Data Algorithm** 
module.exports = {

  filterData: (dataList, userPassedFilter)=>{
    let filteredData = [];

    filteredData = dataList.filter((data) =>{
    for(let item of userPassedFilter){
        for(let key in item){
          if(data[key] === undefined || data[key] !== item[key])
          return false
        }
        


      }
return true
    })

return filteredData

}
}

CodePudding user response:

filter over the dataList and return every object where some of its object properties are included in the userFilter.

const userPassedFilter = [{voltage:122},{phase:1},{phase:2},{voltage:44}];

const dataList = [
  {voltage:44, phase:12},    
  {voltage:35, phase:2},
  {voltage:12, phase:2},
  {voltage:12, phase:3},
  {voltage:35, phase:1},
  {voltage:122, phase:156}
];

function filterData(dataList, userPassedFilter) {
  return dataList.filter(obj => {
    return userPassedFilter.some(inner => {
      const [[ key, value ]] = Object.entries(inner);
      return obj[key] === value;
    });
  });
}

console.log(filterData(dataList, userPassedFilter));

CodePudding user response:

Introduce variable for each loop, so the result will match any of the elements in the userPassedFilter by matching all properties for the element.

let userPassedFilter = [{"phase":1},{"phase":2}];
let dataList = [{"voltage":35, "phase":2},{"voltage":12, "phase":2},{"voltage":12, "phase":3},{"voltage":35, "phase":1}];

const f = (dataList, userPassedFilter) => {
  let filteredData = dataList.filter((data) => {
    let filtered = true;
    for (let item of userPassedFilter) {
      let matched = true;
      for (let key in item) {
        if (data[key] === undefined || data[key] !== item[key]) {
          matched = false;
          break;
        }
      }
      if (matched) {
        filtered = false;
        break;
      }
    }
    return !filtered;
  });
  return filteredData;
};

console.log(f(dataList, userPassedFilter));

A shorter version:

let userPassedFilter = [{"phase":1},{"phase":2}];
let dataList = [{"voltage":35, "phase":2},{"voltage":12, "phase":2},{"voltage":12, "phase":3},{"voltage":35, "phase":1}];

const f = (dataList, userPassedFilter) => {
  let filteredData = dataList.filter((data) => {
    return userPassedFilter.some(item => Object.entries(item).every(([key, value]) => data[key] === value))
  });
  return filteredData;
};

console.log(f(dataList, userPassedFilter));

CodePudding user response:

try adding a variable

filteredData = dataList.filter((data) => {
   let myvar = true;
   for(let item of userPassedFilter){
      for(let key in item){
         if(data[key] === undefined || data[key] !== item[key])
          myvar = false
      }
   }
   return myvar
})

CodePudding user response:

try adding a variable to hold the values:

let userPassedFilter = [{
  "phase": 1
}, {
  "phase": 2
}]

let dataList = [{
    "voltage": 35,
    "phase": 2
  },
  {
    "voltage": 12,
    "phase": 2
  },
  {
    "voltage": 12,
    "phase": 3
  },
  {
    "voltage": 35,
    "phase": 1
  }
]


function filterData(dataList, userPassedFilter) {
  let filteredData = [];

  filteredData = dataList.filter((data) => {
    var throwVar = 0 //add this variable to hold the value
    for (let item of userPassedFilter) {
      for (let key in item) {
        if (data[key] === undefined || data[key] !== item[key]) {
          throwVar = 0
        } else {
          return true
        }
      }



    }
    if (throwVar == 0) {
      return false
    }
  })

  return filteredData

}


console.log(filterData(dataList, userPassedFilter))

  • Related