retrieve the key:value pairs contained within sub-blocks

After receiving the following output from Postman or hitting an endpoint, the challenge is to extract "id" and "name" from the values. The key-value pairs are nested within sub-blocks, so how can this be achieved using JavaScript and then implemented in the "Tests" tab of Postman?

{
  "SearchResult": {
    "total": 11,
    "resources": [
      {
        "id": "12345",
        "name": "GuestType",
        "description": "Identity group ",
      },
      {
        "id": "56789",
        "name": "Admin",
        "description": "",
      },
    ]
  }
}

Answer №1

var data={
  "ItemsList": {
    "count": 11,
    "items": [
      {
        "id": "12345",
        "name": "GuestType",
        "description": "Identity group ",
      },
      {
        "id": "56789",
        "name": "Admin",
        "description": "",
      },
    ]
  }
}

data.ItemsList.items.forEach((item)=>console.log(item.id,item.name));

Answer №2

Discover a simple way to extract an array of objects containing only the id and name properties.... Keep coding with joy :)

let data = {
  "SearchResult":
  {
    "total": 11,
    "resources": [
      { "id": "12345", "name": "GuestType", "description": "Identity group ", },
      { "id": "56789", "name": "Admin", "description": "", }
    ]
  }
}

let ids = data.SearchResult.resources.map(obj => {
  id: obj.id,
  name: obj.name
});

Answer №3

Give it a shot

const idsList = data.SearchResult.resources.map(obj => obj.id);
const namesList = data.SearchResult.resources.map(obj => obj.name);

let data={ "SearchResult": { "total": 11, "resources": [ { "id": "12345", "name": "GuestType", "description": "Identity group ", }, { "id": "56789", "name": "Admin", "description": "", }, ] } }

const idsList = data.SearchResult.resources.map(obj => obj.id);
const namesList = data.SearchResult.resources.map(obj => obj.name);

console.log(idsList);
console.log(namesList);

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

Vue.js The mp3 files have been successfully added to an array, yet the audio remains silent

While coding, I faced an issue that I resolved by doing some research online and tweaking the sample code. As I added more lines of code, I encountered sections that were a bit confusing to me, but I managed to make them work. My main objective is to devel ...

Transferring cookies across subdomains

I am facing an issue with an ajax request going from one subdomain to another, for example from sub1.example.com to sub2.example.com. Despite having a cookie set for all domains (cookie domain='.example.com'), the cookie is not being sent to the ...

Convert symfony serialized data into an object mapped with alternate keys

When I receive data from an API, it looks like this: { "type": "string", "code": "string", "addInfo2": "", "addInfo3": "23536723462", "addInfo4": null, &q ...

Expanding functionality: Steps to integrating a new endpoint into your AWS Amplify Express Server

I have created a REST express server using Amplify. Attempted to include two additional endpoints: // incorporating serverless express app.post('/myendpoint', function(req, res) { console.log('body: ', req.body) res.json(req.body) ...

How to Implement Drupal.behaviors for Specific Pages?

Currently, I have a module that showcases an alert to users within a block. For those interested, you can find my code on GitHub here: https://github.com/kevinquillen/User-Alerts If you would like more information about the module and its functionality, ...

Creating a jQuery countdown script

Is it possible to easily convert the function below into jQuery and still maintain the ability to call multiple instances of the countdown? This particular function receives a server time echoed by the server and then initiates a countdown to the specifie ...

Autocomplete is failing to provide any results

I am experiencing an issue with the autocomplete script as it is not displaying any names under the input field. Below is the code snippet from my index file: <head> <html lang="en"> <meta charset="utf-8"> <meta na ...

Limit the number input to only allow values between 0 and 100

I am utilizing the Number input component from MUI, which includes Increment and Decrement buttons for adjusting numbers. Is there a method to limit the input to only accept values ranging from 0 to 100 ? Additionally, how can I decrease the width of the ...

Addressing the delay of "Rasterize Paint" on mobile devices while implementing css3 opacity transitions

I'm currently working on a project that involves users navigating back and forth between modals. To achieve this, I decided to use CSS transitions to change the opacity from 0 to 1. However, I've encountered some issues with slow transitions. So ...

Explore the feature of toggling visibility of components in Vue.js 3

I am facing an issue with showing/hiding my sidebar using a button in the navbar component. I have a navbar and a side bar as two separate components. Unfortunately, none of the methods I tried such as v-show, v-if, or using a localStorage value seem to wo ...

To determine if an AJAX request is synchronous or asynchronous using Browser Developer Tools

Is there a method to verify if a specific ajax request is asynchronous or synchronous using Browser Dev Tools such as Chrome Developer Tools or Firebug? The HTTP Request Header for an ajax request does not specify whether it is sync or async. X-Request ...

Custom ListView Adapter failing to incorporate all items from a for Loop()

Hey there, I'm currently working with a JSON array that looks like this: {"id":"1114","user_id":"60","paq_type":"Electronics","description":"Karen Brso","title":"xvxcv","notes":"sdf","price":"410.00","size_x":"234","size_y":"234","size_z":"23","weigh ...

Slow loading time when switching between items in the drop-down menu from the Main Menu

Visiting my website at europebathroom.com, you will notice a horizontal main menu with dropdown functionality. When hovering over a menu item, the corresponding dropdown appears seamlessly. Yet, sometimes quickly touching another menu item unintentionally ...

Saving Selected Radio button values into Jquery Array

In my HTML table, there are multiple rows with radio buttons representing the sexes in the 0th position of <td>. I am trying to store the values of sex (either 1 or 0) in an array. Below is a snippet for a table with 3 rows. HTML Code: <table> ...

What steps should be taken to fix the sinopia installation error?

While operating on redhat5.9, I encountered an exception related to 'make'. I am curious about what they are trying to create. It seems like it's related to JavaScript. [root@xxxx bin]# npm install -g sinopia --python=/usr/local/clo/ven/pyt ...

What is the best method for resizing an SVG according to the size of my website's window?

I am attempting to implement a scalable settings icon SVG file that adjusts based on the window width. My approach involved creating a JavaScript function that alters the element's dimensions according to the window size, but unfortunately, this metho ...

Resolving CORS issues: Troubleshooting communication between a React app and an Express server

After successfully running both the app and server locally, I encountered an issue upon deploying the express server. Whenever I try to make a request, I consistently receive the following error message: "has been blocked by CORS policy: Response to ...

Does type conversion have a specific ranking of preference?

When working with JavaScript, it's important to note that the language automatically converts types when necessary. For instance, if you have an expression like "8" * "3", JavaScript will convert the strings to numbers and calculate the result as 24. ...

Tips for Elevating State with React Router Version 6

Looking for advice on sharing state between two routes in my project. I'm debating whether to lift state up from my AddContact component to either the Layout or App components in order to share it with the ContactList. The Layout component simply disp ...

A guide on organizing nested JSON objects in JavaScript

I am currently using JavaScript to retrieve JSON data that looks like this: [{ "data": { "serialNumber": "12345678", "loopCount": 2, "temperature3": 22.74921781259558, "temperature2": 21.459065450414467, "temper ...