【问题标题】:Access _latestValue elements of an Observable Array访问 Observable 数组的 _latestValue 元素
【发布时间】:2020-06-06 21:22:29
【问题描述】:

有一个在控制台中打印的数组,如图所示

console.log(quote);

如何在 js 中访问 quote.shippingAddress() latestValue[countryID]?

正如@harmandeep-singh-kalsi 建议的console.log(quote.shippingAddress.subscribe(data => {data._latestValue }));

然而

quote.shippingAddress.subscribe(data => {data._latestValue.countryId });

返回错误无法读取未定义的属性'countryId'

【问题讨论】:

  • 试试quote.shippingAddress().subscribe(data => {data._latestValue.countryId });
  • @Harmandeep Singh Kalsi 得到 TypeError: Cannot read property 'subscribe' of null
  • quote.shippingAddress.subscribe(data => {data._latestValue.countryId });你也可以发布你的代码sn-p,看看吗?

标签: javascript arrays knockout.js observable


【解决方案1】:

你那里没有访问权限吗? console.log(quote.shippingAddress()['countryId']); 您的shippingAddressobservable,因此您需要调用它来获取“_lastestValue

【讨论】:

  • 你的代码返回 ReferenceError: countryId is not defined
  • 对了,我忘了引号
【解决方案2】:

您的问题“访问可观察数组的 _latestValue 元素”对我来说意味着 shippingAddress 应该是可观察数组。

看起来 shippingAddress 是一个可观察的函数,而不是可观察的数组。

如果您告诉我们您想要做什么,我们可以提供更多帮助。通常你不想直接访问 _latestValue 而是像这样引用 shippingAddress() 。

【讨论】:

  • 谢谢,通常是的。 'countryID' 之类的数据应该在 quote.shippingAddress() 中;但它是空的。在我的环境中,我需要访问 console.log 中显示的“_latestValue”,它确实包含我需要的所有数据。
猜你喜欢
  • 2022-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 2016-06-09
相关资源
最近更新 更多