Home > Blockchain >  JS update the value of the filtered object using array.filter
JS update the value of the filtered object using array.filter

Time:05-18

So I need to perform two actions 1) filter my array and 2) update the unfiltered values. currently, I am doing it this way and it works fine.

let installData = state.installData.filter((item) => {
        return item.sensor !== action.data.sensor;
      });

      installData = installData.map((item) => {
        return { ...item, asOfDate: false };
      });

however, I am doing operations here and was wondering if there is a more efficient way to update the value of my item directly inside the filtred method without having to use map

CodePudding user response:

I believe you could just use reduce and combine operations into a single function

 let installData = state.installData.reduce((acc,item) => {
     if (item.sensor !== action.data.sensor) acc.push({...item, asOfDate: false});
     return acc;
  },[]);
  • Related