我正在对JSON进行AJAX请求,并获得以下代码作为响应:

{
  total: "1",
  items: [
    {
      id: 43,
      title: "ThisIsTheTitle",
      promoted: false,
      sticky: false,
      weight: 10,
      created: {
        timestamp: 1482054,
        formatted: "17/01/2017"
      },
      url: "http://...",
      airdate: {
        timestamp: 1484980,
        formatted: "17/01/2017"
      },
      video: {
        id: 43,
        number_of_views: 1,
        duration: {
          seconds: 50,
          formatted: "00:00"
        },
        required_login: false
      },
      program: {
        id: 25,
        url: "http://...",
        title: "ProgrammaTitel"
      },
      image: {
        uri: "public://...",
        full: "http://..."
      },
      tags: [
        {
          id: "4",
          name: "Map"
        },
        {
          id: "7",
          name: "Aflevering2"
        }
        ]
    }
  ]
}


现在,我将这些数据推送到我自己的JSArray中。请注意,现在只有1个响应项目,但还会添加更多。

我想根据对象(item > tags > name = 'Aflevering2')的标签名称检索特定的对象数据。

所以我想从标记名称为'Aflevering2'的对象中获取数据。

有什么建议吗?谢谢!

最佳答案

您可以找到包含filter()some()组合的项目:

obj.items.filter(v => v.tags.some(k => k.name === 'Aflevering2'));




let obj = {
    total: "1",
        items: [
    {
        id: 43,
        title: "ThisIsTheTitle",
        promoted: false,
        sticky: false,
        weight: 10,
        created: {
            timestamp: 1482054,
            formatted: "17/01/2017"
        },
        url: "http://...",
        airdate: {
            timestamp: 1484980,
            formatted: "17/01/2017"
        },
        video: {
            id: 43,
            number_of_views: 1,
            duration: {
                seconds: 50,
                formatted: "00:00"
            },
            required_login: false
        },
        program: {
            id: 25,
            url: "http://...",
            title: "ProgrammaTitel"
        },
        image: {
            uri: "public://...",
            full: "http://..."
        },
        tags: [
            {
                id: "4",
                name: "Map"
            },
            {
                id: "7",
                name: "Aflevering2"
            }
        ]
    }
]
}

let items = obj.items.filter(v => v.tags.some(k => k.name === 'Aflevering2'));
console.log(items);

关于javascript - Javascript多维数组键值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41694057/

10-09 17:27