0

Jade テンプレートを使用して、Harp プロジェクトに取り組んでいます。コンテンツが入力されていますが、_data.json思ったようにアクセスできません。この JSON 構造を考えると:

{
"mountain-residence": {
    "slug": "mountain-residence",
    "title": "Mountain Residence",
    "lead": "A southeast facing home...",
    "thumb": "exterior/entry",
    "sections": [
      {
        "exterior": {
          "slug": "exterior",
          "title": "Exterior Photos",
          "lead": "Embracing an entry...",
          "thumb": "terrace",
          "photos": [
            {
              "slug": "southeast",
              "alt": "Mountain Room Overlook",
              "caption": "Porch, deck and spa terrace"
            },
            {
              "slug": "terrace",
              "alt": "Southeast Terrace",
              "caption": "Spa deck and landscape terrace"
            },
            {
              "slug": "entry",
              "alt": "Entry Courtyard",
              "caption": "Entry court and pergola"
            },
            {
              "slug": "porch",
              "alt": "Entry Porch",
              "caption": "Timber entry shelter"
            }
          ]
        }
      },
      {
        "interior": {
          "slug": "interior",
          "title": "Interior Photos",
          "lead": "The interior spaces...",
          "thumb": "mountain-room2",
          "photos": [
            {
              "slug": "mountain-room2",
              "caption": "Entry opening to the Mountain Room"
            },
            {
              "slug": "dining",
              "caption": "Dining Room into Mountain Room"
            },
            {
              "slug": "cellar1",
              "caption": "Wine Cellar and Tasting Room"
            },
            {
              "slug": "den",
              "caption": "Den and Family Hearth"
            }
          ]
        }
      },
      {
        "design-sketches": {
          "slug": "design-sketches",
          "title": "Design Sketches",
          "lead": "A careful site...",
          "thumb": "shower",
          "photos": [
            {
              "slug": "schematic",
              "caption": "Exterior Elevation Study"
            },
            {
              "slug": "elevation",
              "caption": "Elevation Color Studies"
            },
            {
              "slug": "shower",
              "caption": "Outdoor stone shower"
            }
          ]
        }
      }
    ]
  }
}

mountain-residence.sections.exterior.title("Exterior Photos")のように、配列内の名前付きオブジェクトにアクセスする方法がわかりません。努力してもうまくいきmountain-residence.sections['exterior'].titleません。で同じプロパティにアクセスできmountain-residence.sections[0].title、JSON は有効なようですが、その名前でオブジェクトを呼び出す必要があります...これを行う方法はありますか?

たとえば"mountain-residence"、ページに入力します。私の一般的なアイデアは、各オブジェクトに"sections"独自のサブページを入力させることでした。そのため、JSON を介して名前で呼び出すことができるようにしたいと考えています。

4

1 に答える 1

0

コメントで述べたように、セクションは配列であるため、インデックス表記のみが機能します。セクションの名前で取得したい場合は、ネイティブにオブジェクトにするか、記述したコードを前処理してオブジェクトに変換する必要があります。

于 2014-08-10T23:38:48.793 に答える