在JavaScript / ReactJS中访问最里面的查询

您好,我想从以下查询中打印最里面的内容, 如何访问计划>个人>标题,然后在屏幕上打印内容。

非常感谢!

我从data.js获取数据,如下所示:

export default [
  {
    sys: {
      id: "1"
    },
    fields: {
      name: "cloud hosting",
      slug: "cloud-hosting",
      type: "hosting",
      price: 100,
      size: 200,
      capacity: 1,
      featured: true,
      description:
        "The power of SSD + Simplicity of cPanel",
      extras: [
        "Plush pillows and breathable bed linens",
        "Soft, oversized bath towels",
        "Full-sized, pH-balanced toiletries",
        "Complimentary refreshments",
        "Adequate safety/security",
        "Internet",
        "Comfortable beds"
      ],
      plans:[
        {
          fields:{
            personal:{
              title: "personal"
            }
          }
        }
      ]
    }
  }
];
评论
沫沫浅言
沫沫浅言

You can make use of flatMap. I'm assuming your data is an array.

var data = [{ sys: { id: "1" }, fields: { name: "cloud hosting", slug: "cloud-hosting", type: "hosting", price: 100, size: 200, capacity: 1, featured: true, description: "The power of SSD + Simplicity of cPanel", extras: [ "Plush pillows and breathable bed linens", "Soft, oversized bath towels", "Full-sized, pH-balanced toiletries", "Complimentary refreshments", "Adequate safety/security", "Internet", "Comfortable beds" ], plans: [{ fields: { personal: { title: "personal" } } }] }}];

result = data.flatMap(({fields})=>fields.plans.map(k=>(k.fields.personal.title)));

console.log(result);
点赞
评论