NGRX还原器集数组到对象内的内部

发布于 2025-01-27 04:18:43 字数 838 浏览 5 评论 0原文

我的存储对象结构如下所示,

{
   "fooObject":[
      {
         "id":1,
         "somefield":"bla bla",
         "orderArray":[
            {
               "id":1,
               "somefield":"a"
            },
            {
               "id":2,
               "somefield":"b"
            },
            {
               "id":3,
               "somefield":"c"
            }
         ]
      }          
   ]
}

我需要使 orderaray 在NGRX还原器上为空。而且只需要更改该数组即可。我试图跟随以获得所需的行为,但没有成功。我也尝试使用地图方法,但它失败了。

on(ProductionLineAction.successSetInitialStateScheduledOrderLine, (state, action) => {
    return {
      ...state,
      fooObject: [
        ...state.productionLinesDisplayDetails,
        {
          orderArray: action.scheduledOrders
        }
      ]
    }
  }),

I have Store object structure as follows

{
   "fooObject":[
      {
         "id":1,
         "somefield":"bla bla",
         "orderArray":[
            {
               "id":1,
               "somefield":"a"
            },
            {
               "id":2,
               "somefield":"b"
            },
            {
               "id":3,
               "somefield":"c"
            }
         ]
      }          
   ]
}

I need to make the orderArray empty on the NGRX reducer. and only need to change that array. I attempted to following to obtain my desired behavior but was unsuccessful.I tried using the Map method as well, but it failed.

on(ProductionLineAction.successSetInitialStateScheduledOrderLine, (state, action) => {
    return {
      ...state,
      fooObject: [
        ...state.productionLinesDisplayDetails,
        {
          orderArray: action.scheduledOrders
        }
      ]
    }
  }),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你另情深 2025-02-03 04:18:43

还原器将冻结状态树中的所有对象,因此您必须创建一个具有新数据副本的新数组。

on(ProductionLineAction.successSetInitialStateScheduledOrderLine, (state, action) => {
    return {
      ...state,
      fooObject: state.fooObject.map(obj => ({
      ...obj, // copy all other old values in each fooObject
      orderArray: [] // replace the order-array with new empty array.
      }))
    }
  }),

The reducer will have frozen all the objects in the state tree so you'll have to create a new array with a new copy of the data.

on(ProductionLineAction.successSetInitialStateScheduledOrderLine, (state, action) => {
    return {
      ...state,
      fooObject: state.fooObject.map(obj => ({
      ...obj, // copy all other old values in each fooObject
      orderArray: [] // replace the order-array with new empty array.
      }))
    }
  }),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文