Key-Value Pairs in a Multidimensional Array using JavaScript

After making an AJAX request to a JSON, I receive the following code as a 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"
        }
        ]
    }
  ]
}

I then store this data in my own JSArray. It's important to note that currently there is only one response item, but more will be added later.

My objective is to extract specific object data based on the tag name of the object

(item > tags > name = 'Aflevering2')

Essentially, I am looking to retrieve the data from the object that has the tag name 'Aflevering2'.

Any suggestions or advice on how to achieve this? Thank you!

Answer №1

To locate specific items, you can utilize 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);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to removing a colon from my JSON.stringify output

I'm encountering an issue with a JSON.stringify function. It is adding a colon after the JSON object. axios.post('http://54.196.61.131/api/v0/user/250/product/add/scan', JSON.stringify({ name: currentProduct.product. ...

Achieving a bookshelf model by combining data from several tables simultaneously

In the process of creating a node.js web service with the functionality of an auction, I am utilizing bookshelf.js for models and MySQL for the database. The database structure to consider is as follows: users: id | login | password | ...

Tips for maintaining consistency between server-side Java and client-side JS DTO properties

Greetings, I am in search of a solution or plugin within Eclipse that can ensure synchronization between server-side Java DTO properties and their corresponding client-side JSON elements as the codebase evolves. For instance, in a web application with a Ja ...

Unravel a PySpark dataframe with certain columns represented as arrays of nested structures

I currently have a dataframe structured like this : root |-- first_name: string |-- last_name: string |-- details: array | |-- element: struct | | |-- university: string | | |-- subjects: struct | | | |-- subject1: string | | ...

Django and the use of global variables across multiple connections

After successfully developing a Django web application and testing it locally, I encountered a setback when trying to make it accessible via the public Internet. The client-side interacts with the server using Ajax to execute Python scripts and display re ...

Froala text area is unexpectedly visible when I attempt to cover it with a partially see-through mask

The website I'm currently developing features a semi-transparent overlay that dims the screen with a light-colored message displayed on top. This overlay and message are shown whenever there is a background process running. Everything was running smoo ...

Identify a 407 error and continue to maintain it for all future requests

I have a straightforward JavaScript code using $.ajax() from JQuery that functions well for both GET and POST requests. However, some users behind a proxy encounter a 407 error, as discussed in the following post. Here I have updated the post to suggest ...

Utilizing the `theme` property in makeStyles with Material-UI

I am currently working on passing down an muiTheme to a component through the use of ThemeProvider, and I also want to apply it to the children components. Furthermore, I aim to utilize the theme's properties in both components by creating a classes o ...

What is the best way to wait for a series of subscriptions to complete?

I am currently facing challenges with Observables while working on a complex REST API query function that involves intricate logic and multiple requests and responses. Although I have already written numerous functions with subscriptions like the ones bel ...

Error encountered in React Native packager due to naming conflict between "lodash" and "yeoman-generator" libraries

Issue Description Within my current project, I am utilizing "react-native": "0.36.0" along with the following dependencies: "lodash": "^4.15.0" "yeoman-generator": "^0.24.1" Upon using versions above "^3.10.1" for "lodash" and "0.21.2" for "yeoman-gene ...

The flickering problem with HTML5 DOM

I created a HTML5 game using canvas and DOM elements, but I'm facing an issue with flickering DOM elements while playing. The problem seems to be more prevalent in mobile browsers, particularly Chrome. My game includes a full screen canvas with vario ...

Click event handling in child components with Vue 2

I have a basic component with the following structure: <template> <span @click="clickHandler" :class="className"></span> </template> <script> export default { name: "PlusIconComponent", props: { ...

How to share custom data with fb_share feature while displaying box count? (find out more below)

I am trying to implement Facebook sharing with box count on my website. The code I am using is as follows: <div id="fbShare"> <a name="fb_share" type="button_count" expr:share_url="data:post.url" href="http://www.facebook.com/sharer.php">S ...

Tips on running jQuery scripts when a div changes its display style from none to block?

Is there a way to trigger jQuery code when the style of a div transitions from `display: none;` to `display: block;`? I am working with tabs, which is why this div's style changes in this manner. The jQuery code should only be executed when this spec ...

Issues with Angular route links not functioning correctly when using an Array of objects

After hard coding some routerLinks into my application and witnessing smooth functionality, I decided to explore a different approach: View: <ul class="list navbar-nav"></ul> Ts.file public links = [ { name: "Home&quo ...

Ways to retrieve information using fgets()

One of the tasks I need to accomplish involves working with a data file that contains various pieces of information. For example: 3 10.9 1 2.1 10 100.5 //This is a blank line 10 200 The first value in each l ...

jQuery's ability to retain data after making an .ajax() call is limited

Currently, I am facing an issue with setting a global variable to store data received from a $.ajax() call. The variable seems to be getting reset for some reason. I am encountering difficulties in maintaining the value of the variable throughout the proc ...

Encountering a 404 error on Vercel deployment of NextJS

I am currently utilizing a custom express server in conjunction with NextJS. During local development, everything runs smoothly. However, upon deployment to Vercel, I encounter 404 errors when attempting to access my backend API. What could be causing th ...

Displaying JavaScript validation errors on a Bootstrap 4 .btn-group-toggle

I have created a star rating user interface using the Bootstrap 4 .btn-group.btn-group-toggle as shown in the code snippet below: <div class="form-group"> <div class="btn-group btn-group-toggle" data-toggle="buttons" aria-label="Rate this"> ...

Dynamic Content Toggle with AJAX in ASP .NET

I am working on an accordion feature that dynamically generates entries based on a record count obtained from databinding. My goal is to add a handler and trigger a specific action in the C# code behind once users click on a button within the accordion. Th ...