【问题标题】:Angular pick up an item within an arrayAngular 在数组中拾取一个项目
【发布时间】:2021-09-02 08:35:53
【问题描述】:

我正在使用 Angular 来操作 tmdb api,但我无法获取数组内的项目,您能帮帮我吗? 数组返回给我的答案是这样的:

{
    "id": 423108,
    "results": [{
        "id": "608177732da846006e382e45",
        "iso_639_1": "en",
        "iso_3166_1": "US",
        "key": "qc6jN1BcJi0",
        "name": "Official Trailer – Warner Bros. UK & Ireland",
        "site": "YouTube",
        "size": 1080,
        "type": "Trailer"
    }, {
        "id": "6081f2879e45860058f36147",
        "iso_639_1": "en",
        "iso_3166_1": "US",
        "key": "h9Q4zZS2v1k",
        "name": "Official Trailer",
        "site": "YouTube",
        "size": 1080,
        "type": "Trailer"
    }, {
        "id": "60a3f3d8cb75d1003f6cad3f",
        "iso_639_1": "en",
        "iso_3166_1": "US",
        "key": "6Eb1V9gJ5Z4",
        "name": "Chasing Evil Featurette",
        "site": "YouTube",
        "size": 1080,
        "type": "Featurette"
    }, {
        "id": "60a7f244e16e5a003f89fcfb",
        "iso_639_1": "en",
        "iso_3166_1": "US",
        "key": "4GjhydkUMrQ",
        "name": "The Conjuring: The Devil Made Me Do It - Demonic Possession Featurette - Warner Bros. UK",
        "site": "YouTube",
        "size": 1080,
        "type": "Featurette"
    }, {
        "id": "60b65a605c563400782c09c4",
        "iso_639_1": "en",
        "iso_3166_1": "US",
        "key": "5FEdg3FhiGc",
        "name": "Final Trailer – Warner Bros. UK & Ireland",
        "site": "YouTube",
        "size": 1080,
        "type": "Trailer"
    }, {
        "id": "60b6e54aefd3c20041e08f6b",
        "iso_639_1": "en",
        "iso_3166_1": "US",
        "key": "AB9mPsH2z1U",
        "name": "The Conjuring: The Devil Made Me Do It | 2021 | Clip: "
        Mitigating Circumstances " HD",
        "site": "YouTube",
        "size": 1080,
        "type": "Clip"
    }, {
        "id": "60b9622aabf8e2006fb33499",
        "iso_639_1": "en",
        "iso_3166_1": "US",
        "key": "tLFnRAzcaEc",
        "name": "Final Trailer",
        "site": "YouTube",
        "size": 1080,
        "type": "Trailer"
    }, {
        "id": "60be2d10960cde006d905ecf",
        "iso_639_1": "en",
        "iso_3166_1": "US",
        "key": "2V2MmKkddM0",
        "name": "The Conjuring: The Devil Made Me Do It - Teaser",
        "site": "YouTube",
        "size": 1080,
        "type": "Teaser"
    }]
}

我想得到“关键”项目。想法是获取密钥,然后将其与 youtube 链接连接以重定向到 youtube 上的预告片,或者将 youtube 播放器放入应用程序中。我目前正在这样做:

this.clientService.getVideoID(this.id).subscribe(data => this.video = date)

但是我只能访问video.results,不能给video.results.key

【问题讨论】:

  • 这是因为 results 是一个对象数组,因此您需要通过video.results[i].key 访问,其中i 是您希望访问的结果中对象的位置参数。

标签: javascript angular api


【解决方案1】:

这个 JSON 元素似乎有问题,因为它包含“

【讨论】:

    【解决方案2】:

    你可以像这样使用filter()函数

    const myVideo = results.filter(item => item.id === "608177732da846006e382e45")[0]

    请注意,它仅在 id 唯一的情况下才有效,filter 返回一个填充了在条件下返回 true 的项的数组( item.key === "608177732da846006e382e45"),然后我们取第一个一个应该是唯一的。

    然后你就可以访问myVideo.key

    显然用您输入的任何内容替换 "608177732da846006e382e45"

    【讨论】:

      猜你喜欢
      • 2013-06-07
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      相关资源
      最近更新 更多