A basic structure for a JSON array

Perhaps my next question may appear silly, but I'll ask it anyway :)

Here is the structure in question:

{
      "name": "Erjet Malaj",
      "first_name": "Erjet",
      "work": [
        {
          "employer": {
            "id": "110108059015688",
            "name": "Art of Living Foundation"
          },
          "position": {
            "id": "137081429661437",
            "name": "Volunteer"
          },
          "start_date": "0000-00",
          "end_date": "0000-00"
        },
        {

I have successfully retrieved response.data[i].first_name, but how do I go about retrieving the work id?

Answer №1

work is an array that requires iteration.

var work = response.data[i].work,
    worklen = work.length,
    j, workdetails;

for(j = 0; j < worklen; j++){

    //simplify for better understanding
    workdetails = work[j];

    //when prop is either "employer" or "position"
    workdetails[prop].id 
    workdetails[prop].name
}

Answer №2

Have you attempted using

response.data[i].work.position.id
?

Upon further reflection, since work is an array of objects, it should be accessed in this way:

response.data[i].work[0].position.id

Answer №3

If you are referring to the employer id for a specific job

This code snippet retrieves the employer id

response.data[i].work[j].employer.id

An alternative approach could be (based on personal preference)

response.data[i].work[j]['employer'].id

In this scenario, 'j' represents the position of the job in an array. Replace 'j' with 0 to retrieve the first job, 1 for the second, and so on.

Answer №4

Every record in the work array contains an id for both the employer and position of the person. To gather these IDs from each work entry, you can iterate over them using the following code:

var person = response.data[i];
var work = person.work;

for(var j = 0; j < work.length; ++j) {
    var work_entry = work[j];
    console.log(work_entry.employer.id);
    console.log(work_entry.position.id); 
}

The items in the work array are accessed sequentially starting from index 0. Each item is an object, so you can access their properties with either obj_name.property_name or obj_name["property_name"].

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

The blur event is failing to trigger in my custom input component

// The Custom Input component const Box = styled.div``; const InputBox = styled.div<{ width: string }>` display: flex; width: ${(props) => props.width}; height: 42px; justify-content: space-between; align-items: center; padding: 9px ...

Accessing a child field from Firebase in a React Native application

My firebase data is structured like this: "Locations" : { "location01" : { "image" : "https://www.senecacollege.ca/content/dam/projects/seneca/homepage-assets/homepage_intl.jpg", "instructorNa ...

Creating dynamic grids in React.js by utilizing HTML

Currently, I am tackling one of the projects on FCC which is the Game of Life. Prior to diving in, my focus is on figuring out how to render a grid on the page. I aim to modify the table dimensions while ensuring it fits neatly within its container. The ...

What is the best way to convert exponential values to decimals when parsing JSON data?

var value = '{"total":2.47E-7}' var result = JSON.parse(value); Looking to convert an exponential value into decimal using JavaScript - any suggestions? ...

Is there a way to seamlessly update button values in VueJs from an api request without needing to reload the page or manually clicking something? Or perhaps there is an alternative method to achieve this?

I attempted to solve the issue by implementing while(true) within the created method so that it constantly updates by sending frequent requests to Flask. Is there a different approach to updating my value? Vue: let app = Vue.createApp({ data ...

Requesting PayPal for an HTTPS transaction

When attempting to call the Express Checkout Paypal API using $http.get(AngularJS), I encountered error 81002(Method Specified is not Supported). However, when I tried calling the API using the search bar in Google Chrome, I was able to retrieve the token ...

Incorporating images with JavaScript in Jade templates

Currently I have a layout.jade code that I am looking to modify the javascript for. My goal is to load an image onto the page every time it is reloaded using jade. Here is the existing code: html head title= title script(src='/libs/jquery/j ...

Developing a JavaScript program that automatically updates the score in a Tic Tac Toe

I am currently working on developing a "Tic Tac Toe" game, but I have encountered an issue. Everything is functioning properly except for one aspect: when the game concludes and all squares are filled with either O or X, or when either X or O wins, it doe ...

Sending data through a form using AJAX and PHP

Greetings! I've developed a page that allows users to view results for a specific tournament and round. The user will first select a sport, which will then populate the available tournaments based on the sport selection. Following this, the user can ...

Resetting the caret position in a React Native TextInput occurs when switching the secureTextEntry prop

As I develop a component to wrap the React Native TextInput in my app, I encounter an issue with the caret position resetting to 0 when toggling the secureTextEntry prop for password visibility. To address this problem, I implemented a workaround using a s ...

Retrieve a specific KEY VALUE pair from a column in a SQL Server database

Within my SQL Server table, there is a Column named JsonData that contains data formatted as key/value pairs. Here is an example: {"Id":42662,"VersionNumber":0,"IsCustom":false,"SaleType":0,"RedBookSpotId":232164,"RegistrationNumber":"AUTOTEST","Price": ...

exploring the network of connections

This is what the HTML structure of the webpage looks like: <body> <form> <input type='file'/> </form> <div id='list'> <div>value here<input id='delete' type='button'/>< ...

Is it possible to exchange CSS classes for a specific group of elements using JQuery?

I have two list items listed below: <li name="luxury" class="cars luxury> <div id="featured_lux" name="featured" class="carImg_lux col2_lux "> My Luxury Car<img border="0" class="car-im ...

How can I retrieve data submitted in a POST request on my Node.js server

I'm having trouble sending form data to a node/express server using AJAX. After submitting, I keep getting redirected to a 'Cannot POST /' page. Although I can log the request object on the server side, the data from the form is missing. Wha ...

Updating an API parameter in React's setState doesn't reflect the new changes

Having trouble with a web app that I'm currently developing. The issue arises on the recipe page where the search functionality isn't updating as expected. Although it is binded to the Enter key and the request itself works, the state update do ...

Utilize the splice function when resizing the window based on specific breakpoints

On a series of div elements, I have implemented some JS/jQuery code that organizes them by wrapping every three elements in a container with the class .each-row. <div class="element"></div> <div class="element"></div> <div class ...

Using Angular to convert JSON data to PDF format and send it to the printer

Currently, I am retrieving JSON data from an API and now need to convert this data into a PDF format for printing. I am encountering an issue where the CSS styling for page breaks is not rendering properly within my Angular component. When I test the same ...

I can't figure out why my unslider isn't adapting to the screen size. Check out the fiddle for more details

I recently implemented unslider to create a slideshow that spans 100% of the width on my website. Everything seemed to be working fine until I tried resizing the screen, and the slides remained the same size as they were initially loaded. Here is the code ...

Error alert: TypeScript typings issue - Naming conflict with Promise / Failure to locate name Promise

I am currently working on a client/server JavaScript application and I am facing a significant issue with Promises. It appears that they are either undefined or duplicated, and this problem seems to be related to the @types package. npm install --save @ty ...

What could be the reason for Angular showing the raw HTML code instead of

I'm currently diving into Angular and encountering an issue where my HTML isn't properly displaying the values I've set. It appears to only show the raw HTML. Here's a snippet from my HTML page: <p>to-do-items works!</p> < ...