简体   繁体   English

JS使用array.filter更新过滤后的object的值

[英]JS update the value of the filtered object using array.filter

So I need to perform two actions 1) filter my array and 2) update the unfiltered values.所以我需要执行两个操作 1) 过滤我的数组和 2) 更新未过滤的值。 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但是,我在这里进行操作,想知道是否有更有效的方法可以直接在 filtred 方法中更新我的项目的值,而不必使用 map

I believe you could just use reduce and combine operations into a single function我相信您可以使用reduce并将操作组合成一个 function

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM