如何访问嵌套对象数组中的第一个元素?

时间:2020-07-06 10:01:49

标签: javascript

我有以下JSON数据:

[{
  "target": {
    "source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0",
    "selector": {
      "conformsTo": "http://www.w3.org/TR/media-frags/",
      "value": "xywh=pixel:582.6087036132812,114.49275207519531,98.55072021484375,189.85508728027344",
      "type": "FragmentSelector"
    }
  },
  "id": "#423647a0-bd23-11ea-8727-73bc60edcd34",
  "body": [{
    "created": "2020-07-03T11:49:32.058Z",
    "purpose": "commenting",
    "type": "TextualBody",
    "creator": {
      "name": "testme",
      "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2"
    },
    "value": "don't move me"
  }, {
    "creator": {
      "name": "testme",
      "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2"
    },
    "value": "move",
    "type": "TextualBody",
    "purpose": "tagging",
    "created": "2020-07-03T11:49:30.850Z"
  }],
  "@context": "http://www.w3.org/ns/anno.jsonld",
  "type": "Annotation",
  "photoDocId": "92wNwz2aaqy7CWf3mGo1"
}, {
  "@context": "http://www.w3.org/ns/anno.jsonld",
  "body": [{
    "type": "TextualBody",
    "purpose": "commenting",
    "created": "2020-07-03T10:57:40.590Z",
    "value": "anyone ever climb this mountain top?",
    "creator": {
      "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92",
      "name": "doss"
    }
  }, {
    "created": "2020-07-03T10:57:39.351Z",
    "value": "ridge",
    "type": "TextualBody",
    "creator": {
      "name": "doss",
      "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92"
    },
    "purpose": "tagging"
  }, {
    "value": "i did in 2005",
    "purpose": "commenting",
    "type": "TextualBody",
    "creator": {
      "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2",
      "name": "testme"
    },
    "created": "2020-07-03T10:59:45.318Z"
  }, {
    "type": "TextualBody",
    "purpose": "tagging",
    "creator": {
      "name": "testme",
      "id": "0qbvzjI3llhyIrKVO6PZxcZUSiI2"
    },
    "value": "testme",
    "created": "2020-07-03T10:59:43.966Z"
  }, {
    "value": "test",
    "type": "TextualBody",
    "purpose": "replying",
    "creator": {
      "name": "doss",
      "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92"
    },
    "created": "2020-07-03T11:39:18.860Z"
  }],
  "type": "Annotation",
  "photoDocId": "92wNwz2aaqy7CWf3mGo1",
  "id": "#03a1f0e0-bd1c-11ea-a688-e1387cc6bed2",
  "target": {
    "selector": {
      "value": "xywh=pixel:247.82608032226562,73.91304016113281,233.33334350585938,240.57972717285156",
      "type": "FragmentSelector",
      "conformsTo": "http://www.w3.org/TR/media-frags/"
    },
    "source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0"
  }
}, {
  "body": [{
    "value": "test",
    "created": "2020-07-05T11:29:14.742Z",
    "purpose": "commenting",
    "creator": {
      "name": "doss",
      "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92"
    },
    "type": "TextualBody"
  }, {
    "creator": {
      "name": "doss",
      "id": "K2Lb1R7owqR9BYmpJAJzrg6w1s92"
    },
    "created": "2020-07-05T11:29:13.584Z",
    "value": "test",
    "purpose": "tagging",
    "type": "TextualBody"
  }],
  "type": "Annotation",
  "target": {
    "selector": {
      "conformsTo": "http://www.w3.org/TR/media-frags/",
      "value": "xywh=pixel:395.65216064453125,413.0434875488281,142.02899169921875,131.88406372070312",
      "type": "FragmentSelector"
    },
    "source": "https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0"
  },
  "photoDocId": "92wNwz2aaqy7CWf3mGo1",
  "id": "#c1761960-beb2-11ea-888f-c3ab54e1f3a9",
  "@context": "http://www.w3.org/ns/anno.jsonld"
}]

此数据是图像的注释数据。

我想做的是循环遍历每个对象的body,并且仅检查第一个元素(即body[0].creator.id)的创建者字段。我想检查该id字段中的creator值。如果ID是原始作者的ID(在此示例中,请使用ID 0qbvzjI3llhyIrKVO6PZxcZUSiI2),则返回console.log('original author')

在访问上述元素时,我需要帮助。有提示吗?

侧面说明:进行上述工作流程的原因是因为我需要防止其他用户编辑作者的原始注释。原始作者将始终是每个body中的第一个元素。

1 个答案:

答案 0 :(得分:0)

您可以尝试

const data = [
  {
    target: {
      source:
        'https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0',
      selector: {
        conformsTo: 'http://www.w3.org/TR/media-frags/',
        value:
          'xywh=pixel:582.6087036132812,114.49275207519531,98.55072021484375,189.85508728027344',
        type: 'FragmentSelector'
      }
    },
    id: '#423647a0-bd23-11ea-8727-73bc60edcd34',
    body: [
      {
        created: '2020-07-03T11:49:32.058Z',
        purpose: 'commenting',
        type: 'TextualBody',
        creator: { name: 'testme', id: '0qbvzjI3llhyIrKVO6PZxcZUSiI2' },
        value: "don't move me"
      },
      {
        creator: { name: 'testme', id: '0qbvzjI3llhyIrKVO6PZxcZUSiI2' },
        value: 'move',
        type: 'TextualBody',
        purpose: 'tagging',
        created: '2020-07-03T11:49:30.850Z'
      }
    ],
    '@context': 'http://www.w3.org/ns/anno.jsonld',
    type: 'Annotation',
    photoDocId: '92wNwz2aaqy7CWf3mGo1'
  },
  {
    '@context': 'http://www.w3.org/ns/anno.jsonld',
    body: [
      {
        type: 'TextualBody',
        purpose: 'commenting',
        created: '2020-07-03T10:57:40.590Z',
        value: 'anyone ever climb this mountain top?',
        creator: { id: 'K2Lb1R7owqR9BYmpJAJzrg6w1s92', name: 'doss' }
      },
      {
        created: '2020-07-03T10:57:39.351Z',
        value: 'ridge',
        type: 'TextualBody',
        creator: { name: 'doss', id: 'K2Lb1R7owqR9BYmpJAJzrg6w1s92' },
        purpose: 'tagging'
      },
      {
        value: 'i did in 2005',
        purpose: 'commenting',
        type: 'TextualBody',
        creator: { id: '0qbvzjI3llhyIrKVO6PZxcZUSiI2', name: 'testme' },
        created: '2020-07-03T10:59:45.318Z'
      },
      {
        type: 'TextualBody',
        purpose: 'tagging',
        creator: { name: 'testme', id: '0qbvzjI3llhyIrKVO6PZxcZUSiI2' },
        value: 'testme',
        created: '2020-07-03T10:59:43.966Z'
      },
      {
        value: 'test',
        type: 'TextualBody',
        purpose: 'replying',
        creator: { name: 'doss', id: 'K2Lb1R7owqR9BYmpJAJzrg6w1s92' },
        created: '2020-07-03T11:39:18.860Z'
      }
    ],
    type: 'Annotation',
    photoDocId: '92wNwz2aaqy7CWf3mGo1',
    id: '#03a1f0e0-bd1c-11ea-a688-e1387cc6bed2',
    target: {
      selector: {
        value:
          'xywh=pixel:247.82608032226562,73.91304016113281,233.33334350585938,240.57972717285156',
        type: 'FragmentSelector',
        conformsTo: 'http://www.w3.org/TR/media-frags/'
      },
      source:
        'https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0'
    }
  },
  {
    body: [
      {
        value: 'test',
        created: '2020-07-05T11:29:14.742Z',
        purpose: 'commenting',
        creator: { name: 'doss', id: 'K2Lb1R7owqR9BYmpJAJzrg6w1s92' },
        type: 'TextualBody'
      },
      {
        creator: { name: 'doss', id: 'K2Lb1R7owqR9BYmpJAJzrg6w1s92' },
        created: '2020-07-05T11:29:13.584Z',
        value: 'test',
        purpose: 'tagging',
        type: 'TextualBody'
      }
    ],
    type: 'Annotation',
    target: {
      selector: {
        conformsTo: 'http://www.w3.org/TR/media-frags/',
        value:
          'xywh=pixel:395.65216064453125,413.0434875488281,142.02899169921875,131.88406372070312',
        type: 'FragmentSelector'
      },
      source:
        'https://firebasestorage.googleapis.com/v0/b/vue-photoapp-api.appspot.com/o/photos%2Fmountains-hero.jpg?alt=media&token=fbe93188-d13d-4a7f-a472-4a529aa565a0'
    },
    photoDocId: '92wNwz2aaqy7CWf3mGo1',
    id: '#c1761960-beb2-11ea-888f-c3ab54e1f3a9',
    '@context': 'http://www.w3.org/ns/anno.jsonld'
  }
]

data.forEach(d => {
  if (d.body[0].creator.id === '0qbvzjI3llhyIrKVO6PZxcZUSiI2') {
    console.log('original author')
  }
})