简体   繁体   English

JavaScript 数组对象未打印到控制台

[英]JavaScript Array objects not printing to console

I have a code snippet that is suppose to print array object to log but not working wondering if someone could help.我有一个代码片段,假设打印数组 object 以记录但无法正常工作,想知道是否有人可以提供帮助。 I am trying to get the subscriptionExpirationDate and item我正在尝试获取 subscriptionExpirationDate 和项目

What works什么有效

const onSessionConnect = (event) => {
    if (event.purchaseState == 'PURCHASED') {
       console.log('plan list is found here ', event.PurchaseState);
       
    }

Console prints Purchased购买的控制台印刷品

What Does not work什么不起作用

const onSessionConnect = (event) => {
    if (event.purchaseState == 'PURCHASED') {
       console.log('plan list is found here ', event.purchases.skuIdentifier);
       
    }

console prints Undefined控制台打印未定义

Could someone explain how to get the other objects to print in the console有人可以解释如何让其他对象在控制台中打印

Here is the array这是数组

{
  purchases: [{
    skuIdentifier: '199_1m_1w0',
    subscriptionExpirationDate: '2020-11-15T06:12:57Z',
    purchaseSource: 'USER',
    transactionIdentifier: 'BPS-74511616-4E51-42F7-A528-DE15A8FF0279'
  }],
  purchaseState: 'PURCHASED'
}

purchases is an array and you are trying to access it like the object. You can access it like purchases是一个数组,您正在尝试像 object 一样访问它。您可以像这样访问它

console.log('plan list is found here ', event.purchases[0].skuIdentifier);

purchases is an array of JSON objects and hence you need to access 0th index of purchases. purchases是一个包含 JSON 个对象的数组,因此您需要访问第 0 个 purchases 索引。

console.log('plan list is found here ', event.purchases[0].skuIdentifier);

Whenever you are stuck in these kind of issues, print out the main object in console.log and then you will see the complete definitition of the object in Developer console.每当你遇到这类问题时,在 console.log 中打印出主要的 object,然后你将在开发者控制台中看到 object 的完整定义。

As you can see here, purchases has 1 in the brackets followed by square brackets which signifies that x.purchases is an array with 1 element in it.正如您在此处看到的,purchases 在括号中有 1,后面是方括号,这表示 x.purchases 是一个包含 1 个元素的数组。

在此处输入图像描述

And if you expand this, Developer console will show it in a nice way as follows如果你展开它,开发者控制台将以一种很好的方式显示它,如下所示

在此处输入图像描述

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

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