简体   繁体   中英

Javascript Multidimensional Array Key-Value

I am doing an AJAX request to a JSON and getting following code as response:

{
  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"
        }
        ]
    }
  ]
}

Now I push this data into my own JSArray. Note there is now only 1 response-item but more will be added.

I want to retrieve specific object-data based on the name of a tag of the object (item > tags > name = 'Aflevering2')

So I would like the data from the object where the tag name is 'Aflevering2' .

Any advice? Thanks!

You can find the items with a combination of filter() and 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); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM