简体   繁体   English

MobX将对象添加到可观察数组

[英]MobX adding objects to an observable array

MobX objects to an observable array MobX对象到可观察数组

I am unable to push an object into an observable array and have it be something that I can iterate on. 我无法将一个对象推入一个可观察的数组,并且无法对其进行迭代。

starting point (all data is via the console's log): 起点(所有数据均通过控制台日志):

if (!self.selectedGlobalFilters) self.selectedGlobalFilters = observable([])

and taking in an object filter.options01 as: 并将对象filter.options01作为:

Proxy {Symbol(mobx administration): ObservableObjectAdministration$$1}
  [[Handler]]: Object
    [[Target]]: Object
      key: "status"
      value: "rejected"

...and then attempting a push ...然后尝试推送

self.selectedGlobalFilters.push(filter.options01)

gives me: 给我:

[Proxy]
  0: Proxy
    [[Handler]]: Object
      [[Target]]: Object
        key: "status"
        value: "rejected"

then attempting to push another object filter.options02 as: 然后尝试将另一个对象filter.options02推送为:

Proxy {Symbol(mobx administration): ObservableObjectAdministration$$1}
  [[Handler]]: Object
    [[Target]]: Object
      key: "status"
      value: "done"


self.selectedGlobalFilters.push(filter.options02)

gives me: 给我:

[Proxy]
  0: Proxy
    [[Handler]]: Object
      [[Target]]: Object
        key: "status"
        value: "done"

So basically 2 issues here, one being that the second push overrides the first, but more-so I would like to have this data as such: 因此,这里基本上有2个问题,一个是第二个推送覆盖了第一个推送,但更多,所以我想拥有这样的数据:

[{…}]
  0: 
    status: "rejected"
  1:
    status: "done"

TLDR; TLDR; How would I go about achieving this? 我将如何实现这一目标? pushing an object into an observable array in a manner that can be iterated on... 以可迭代的方式将对象推入可观察的数组...

slicing the array has no different effect on the output. 切片数组对输出没有不同的影响。

As always any and all direction is appreciated so thanks in advance! 一如既往地感谢所有方向,在此先感谢您!

I believe the issues here is that you're only observing a variable and not Mapping the entire array! 我相信这里的问题是,您只观察一个变量,而不是映射整个数组!

@observable selectedGlobalFilters = [];

if (!this.selectedGlobalFilters.length) 
  this.selectedGlobalFilters.push(filter.options01)

..or ..要么

const gFilter = toJS(this.selectedGlobalFilters)
  this.selectedGlobalFilters = gFilter.push(filter.options01)

..if symptoms persist:-) ..如果症状持续:-)

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

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