Retrieve the values of multiple children nested within a parent JSON object

Within this JSON object snippet, I aim to extract the triggerid of each child object. While I can retrieve the first triggerid using the provided code line,

 var gettrigger = eventdata.result[0].relatedObject.triggerid;

I am unsure how to gather the triggerid of every child and store them in an array. Can you provide guidance on achieving this?

var event = {
   "jsonrpc":"2.0",
   "result":[
      {
         "eventid":"964101",
         "name":"unreachable last 5 minutes",
         "severity":"0",
         "acknowledged":"0",
         "clock":"1578456800",
         "hosts":[
            {
               "hostid":"11195",
               "name":"Vi-console"
            }
         ],
         "relatedObject":{
            "triggerid":"23630"
         }
      },
      {
         "eventid":"964091",
         "name":"unreachable last 5 minutes",
         "severity":"0",
         "acknowledged":"0",
         "clock":"1578456695",
         "hosts":[
            {
               "hostid":"11180",
               "name":"Net-fluid"
            }
         ],
         "relatedObject":{
            "triggerid":"23375"
         }
      },
      {
         "eventid":"964090",
         "name":"high cpu usage",
         "severity":"3",
         "acknowledged":"0",
         "clock":"1578456675",
         "hosts":[
            {
               "hostid":"11188",
               "name":"Net-OFFICE"
            }
         ],
         "relatedObject":{
            "triggerid":"23503"
         }
      }
   ]
}

Answer №1

To extract specific data from an array, you can utilize the .map() function in JavaScript. Here is an example code snippet:

var arr = eventdata.result.map(data => data.relatedObject.triggerid);

var eventdata = {
   "jsonrpc":"2.0",
   "result":[
      {
         "eventid":"964101",
         "name":"unreachable last 5 minutes",
         "severity":"0",
         "acknowledged":"0",
         "clock":"1578456800",
         "hosts":[
            {
               "hostid":"11195",
               "name":"Vi-console"
            }
         ],
         "relatedObject":{
            "triggerid":"23630"
         }
      },
      {
         "eventid":"964091",
         "name":"unreachable last 5 minutes",
         "severity":"0",
         "acknowledged":"0",
         "clock":"1578456695",
         "hosts":[
            {
               "hostid":"11180",
               "name":"Net-fluid"
            }
         ],
         "relatedObject":{
            "triggerid":"23375"
         }
      },
      {
         "eventid":"964090",
         "name":"high cpu usage",
         "severity":"3",
         "acknowledged":"0",
         "clock":"1578456675",
         "hosts":[
            {
               "hostid":"11188",
               "name":"Net-OFFICE"
            }
         ],
         "relatedObject":{
            "triggerid":"23503"
         }
      }
   ]
};

var arr = eventdata.result.map(data => data.relatedObject.triggerid);

console.log(arr)

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

A step-by-step guide to using curl for API authentication

As I dive into the world of creating APIs, I'm in the process of developing one for my PHP site. Currently, my approach involves having a script make a cURL call to a PHP file that handles all the processing. For instance, I may use a POST call to an ...

What are the steps to effectively incorporate Bootstrap into a React project?

I have recently put together a basic webpage with a header and footer. Utilizing Bootstrap for the header components, I have encountered an issue where Bootstrap does not seem to be functioning as expected. Here's a snippet from App.js: import R ...

jquery kwicks problem

I have been grappling with a coding problem for hours on end and I have hit a wall. Assistance is needed. On a staging page, the code was tested and found to be functioning properly. However, on the live page, the same code fails to respond as expected. I ...

Every time I execute my program, fgets() seems to be disregarded and scanf() fails to input any data into the structure

Whenever I execute it, fgets() is simply ignored and scanf() doesn't input data to the structure. This is my code: #include"stdio.h" #include"stdlib.h" typedef struct { char nazv[50]; int numr; int date[3]; int time[2]; } train; void input ...

"Error: Unable to access the property '$emit' of an undefined value" - VueJS

I'm currently working on implementing a basic authentication system in vuejs. I have a set of objects containing valid usernames and passwords. I am looping through this list to validate the entered username and password. If there is a match, I trigge ...

Angular services are equipped with a powerful tool called the $http

Encountering an issue with promises within an angular service. The problem lies with a specific method getArea in the service which aims to retrieve the name of a service area. This data is obtained from the API. However, upon obtaining the service areas a ...

`Integrate Passport Azure AD authentication into your GraphQL Server's Context`

Seeking assistance from experienced individuals to tackle some async JavaScript issues. I am trying to secure a GraphQL server using the Passport library and the passport-azure-ad strategy. The validation of incoming Access Tokens seems to be working fine ...

Prolong the duration before the submenu closes on a div-based css menu

I have created a unique horizontal menu using DIVs without the use of ul and li lists. Currently, I am searching for a way to delay the collapse of the submenus when the mouse moves out. I don't mind if the solution involves JavaScript, jQuery, or CSS ...

Connect two selection boxes' values with a single knockout date attribute

I am currently working with a knockout object that consists of a single property named created. The created property contains a Date object which represents the date of an event. In my HTML form, I have two select box inputs: one for selecting hours and th ...

The issue with executing event.keyCode == 13 in Firefox remains unresolved

I've implemented a function that sends comments only when the "enter" key is pressed, but not when it's combined with the "shift" key: $(msg).keypress(function (e) { if (event.keyCode == 13 && event.shiftKey) { event.stopProp ...

Is there a way to broadcast a message to all the Discord servers where my bot is currently active using v14 of discord.js?

Can someone assist me in creating code that allows me to send a message to all servers at my discretion? I have not found any resources on this topic for discord.py or the newer versions of discord.js ...

I keep encountering a 'Missing Permissions' issue whenever I attempt to execute the ban command in Discord.js. What could be causing this problem?

While working on coding a ban command for my Discord server, I encountered an issue where the command was not working as expected. Upon testing, I received an error message on the console stating DiscordAPIError[50013]: Missing Permissions. This was puzzli ...

Javascript: Harnessing Textbox Arrays for Improved Functionality

Check out the form displayed below. <form id="upload_form" enctype="multipart/form-data" method="post"> <input type="text" name="name[]" id="name"><br> <input type="text" name="name[]" id="name"><br> <input type="fil ...

Guidance on sharing an image on Twitter using Node.js

Every time I attempt to upload a PNG image to the Twit library in Node, an error arises. My objective is to develop a Twitter bot in Node.js that generates a random RGB colour, creates an image of this colour, and tweets it. Thanks to some assistance prov ...

What is the connection between importing and using destructuring in ES6 syntax?

Bring in: import React, { Component } from 'react'; Unpacking: let z, { b } = {a: 1, b: 2, c: 3} Both of these examples seem to have similar syntax. However, in the second example, z will be undefined instead of {a: 1, b: 2, c: 3}. Does this ...

Ways to resolve NullPointerException in an array when transitioning to another method

I am currently developing a chess game in Java that involves creating a 2D array of objects to represent the chessboard. Initially, when the array is created, it contains all the necessary objects. However, upon calling a different method from another obje ...

Add up all the numbers within an array by continuously adding them together

I am currently working on a program for my class that is designed to calculate the sum of all integers in an array using recursion. Below is the code snippet I have developed so far: public class SumOfArray { private int[] a; private int n; private int r ...

Filling out the form will automatically direct you to the text input section

I'm trying to figure out if I can create an input box in HTML that directs the user to a specific HTML file based on the word they enter. For example, if they type "Doctor", it would redirect them to the page doctor.html. This is for a school project ...

Tips for preventing multiple requests in your JavaScript search autocomplete feature

I'm currently using the Turbolinks.visit action with the help of $(document).on("input");... HTML <form id="mainSearch" method="get" autocomplete="off"> <input type="search" name="s" placeholder="Search" /> </form> Javascript ...

Issue with Angularjs: NG-repeat and filter not functioning correctly on array

After spending the last 3 hours searching on Google and Stack Overflow, I thought this would be easy, but I'm still stuck. I have a simple ng-repeat where I need to display everything from data2 that matches the unique ID in my data1's ref_id co ...