如何使用JavaScript获取JSON响应中的嵌套项

huangapple go评论68阅读模式
英文:

How to get nested item in JSON response with JavaScript

问题

我有这个JSON响应:

{ id: 41071539879970, properties: { ... }, quantity: 1, variant_id: 41071539879970, key: '41071539879970:e0eefb458778bcb81e9efbf1fed9aa6b' }
final_price: 1100
selling_plan_allocation:
{
  price: 1100
  selling_plan:
  {
    fixed_selling_plan: false
    id: 1079443490
    name: "每4周交付"
  }
}

这是我得到响应的方式:

const subscriptionProducts = cartResponse.items.filter(item => item.selling_plan_allocation)[0];

然而,我需要获取`item.selling_plan_allocation.selling_plan.name`的值

所以我尝试了:

const subscriptionProducts = cartResponse.items.filter(item => item.selling_plan_allocation.selling_plan.name)[0];

返回undefined。然后我尝试了:

const subscriptionProducts = cartResponse.items.filter(item => item.selling_plan_allocation)[0];
const subscriptionName = subscriptionProducts.selling_plan.name

但这又返回了一个错误:

> 未捕获的(在promise中)TypeError:无法读取未定义的属性
> (读取'name')

有人能告诉我我做错了什么吗?
不明白为什么这不起作用。
英文:

I've got this JSON response:

{id: 41071539879970,properties: {…},quantity: 1,variant_id: 41071539879970, key: '41071539879970:e0eefb458778bcb81e9efbf1fed9aa6b'}
final_price:1100
selling_plan_allocation: 
 {
  price: 1100
  selling_plan: 
   {
    fixed_selling_plan:false
    id:1079443490
    name: "Delivery every 4 weeks”
   }
 }

And this is how I'm getting the response:

const subscriptionProducts = cartResponse.items.filter(item => item.selling_plan_allocation)[0];

However, I need to get the value of item.selling_plan_allocation.selling_plan.name

So I tried:

const subscriptionProducts = cartResponse.items.filter(item => item.selling_plan_allocation.selling_plan.name)[0];

which returned undefined. Then I tried:

const subscriptionProducts = cartResponse.items.filter(item => item.selling_plan_allocation)[0];
const subscriptionName = subscriptionProducts.selling_plan.name

Again this returned an error:

> Uncaught (in promise) TypeError: Cannot read properties of undefined
> (reading 'name')

Can somebody tell me what I'm doing wrong?
Don't understand why this isn't working.

答案1

得分: 1

为了安全地访问嵌套属性,您需要确保沿路径的所有中间属性都有效且不为undefined。以下是在嵌套结构中访问name属性的正确方式:

const subscriptionProducts = cartResponse.items.find(item => item.selling_plan_allocation);

if (subscriptionProducts) {
  const subscriptionName = subscriptionProducts.selling_plan_allocation.selling_plan.name;
  console.log(subscriptionName);
} else {
  console.log("未找到订阅产品");
}

在这段代码中,使用find函数来定位第一个具有selling_plan_allocation的项目。如果找到这样的项目,您可以安全地访问嵌套属性,包括selling_plan.name。

英文:

To access the nested property safely, you need to ensure that all intermediate properties along the path are valid and not undefined. Here's the correct way to access the name property within the nested structure:

const subscriptionProducts = cartResponse.items.find(item => item.selling_plan_allocation);

if (subscriptionProducts) {
  const subscriptionName = subscriptionProducts.selling_plan_allocation.selling_plan.name;
  console.log(subscriptionName);
} else {
  console.log("No subscription products found");
}

In this code, the find function is used to locate the first item that has selling_plan_allocation. If such an item is found, you can safely access the nested properties, including selling_plan.name.

huangapple
  • 本文由 发表于 2023年8月10日 16:03:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76873743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定