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

Managing node names while converting CSV to JSON using Python

Currently, I am utilizing a straightforward python script for converting CSV files to JSON format. Successfully, I have managed to convert the CSV data into JSON by following this process... csvFilePath = ("x.csv") # Reading the CSV file and storing the ...

Retrieve an array of information from a firestore database query

I am encountering an issue while trying to retrieve information about all users who are friends of a specific user. I have attempted to gather the data by pushing it to an array and then returning it as a single JSON array. However, it seems that the dat ...

Enhance Your Website with HTML and JavaScript

Here is the code snippet I am working with: sTAT **javascript** var acc = document.getElementsByClassName("item-wrapper"); var i; for (i = 0; i < acc.length; i++) { acc[i].onclick = function(){ this.classList.toggle("selected"); this.nextElementS ...

Fulfill a pledge after a function has been run five times

I'm struggling to understand why my promise is not working as expected and how to resolve the issue. Here is the code snippet: let count = 0; function onLoad(){ count++ console.log(count); return new Promise((resolve) => { ...

Trouble with jquery/ajax form submission functionality

I followed a jQuery code for form submission that I found on various tutorial websites, but unfortunately, the ajax functionality doesn't seem to be working. When I try to submit the form, nothing happens at all. I've tried troubleshooting in eve ...

What is the best way to set up the correct pathway for displaying the image?

I'm currently facing a challenge with displaying an image from my repository. The component's framework is stored in one library, while I'm attempting to render it in a separate repository. However, I am struggling to determine the correct p ...

Issue: .catch(error) function in Node / Express not returning as expectedDescription: After

I'm currently developing a REST API and focusing on effectively managing all error scenarios. Upon successful completion of the API call, I make sure to return the success object to the calling function and then send the response to the client. Howev ...

Retrieve information about the clicked item using the Backbone framework

I have a unique webpage that showcases an impressive collection of books. Each book listed on the page comes with essential information such as the title, price, and description. This data is imported from a JSON file. Excitingly, when users click on any ...

Tips for implementing resizable Material-UI Dialogs

I'm working on a project that involves creating a dialog box that can be resized and dragged. While I found steps in the Material-UI Dialog documentation on how to make it draggable, I'm still looking for information on how to make it resizable. ...

When transmitting a variable from JavaScript to PHP using Ajax, the data appears to be missing

While attempting to pass a simple string variable using an onclick event, I encountered an issue where the request was successful but the response in the console displayed as empty. Additionally, the xvar variable did not come through properly resulting in ...

Encountering a TypeError when attempting to read the property 'name' of undefined while submitting a form in node.js

I'm currently working on a node Js project and I'm encountering an issue while saving form values to a mongoDB database. I've been troubleshooting but can't seem to pinpoint the cause of this error. The error is occurring at the router. ...

Reformatting the JSON Data in Python Post-Retrieval

I've been facing a challenge with this issue for quite some time now and it has negatively impacted my productivity. Currently, I am utilizing python to initiate a session with an api and retrieve its data. The URL follows the format http://api.kiva ...

Steps to initiate my selenium-standalone server: Execute the command "selenium-standalone start"

I'm currently facing a challenge while trying to execute a test script in WebDriverIO. After cloning the code from wdio-cucumber-framework, I am unable to get selenium-standalone to start properly. The error message indicates an issue with geckodriv ...

Encountered an issue while loading a pretrained JSON model in a JavaScript script within a locally hosted

At the moment, I am using TensorFlow in Python to train a model and saving it as model.json along with the BIN file in a folder named models. My goal is to develop a web application that can load this pre-trained model for prediction. However, I have been ...

Tips for styling a button upon being clicked

Is there a CSS technique to make a button change color when clicked with the mouse? I am aware of using ':hover' for changing color, but I need something specific to a left mouse click. I want the same functionality as a standard button but with ...

Implementing access restrictions for modules in NodeJS

In Node, is it possible to limit access or permit access only to specific modules from a particular module? Should I consider replacing the require function and object in the global scope for this purpose? I have concerns about the security of a certain mo ...

Transforming a file with a node module that lacks a .js class into a browseable format

I am currently working on browserifying my module. One of the dependencies I have is on this https://www.npmjs.com/package/chilkat_win32. It is present in my node_modules folder and here is how the structure looks: https://i.stack.imgur.com/uw9Pg.png Upo ...

Utilize jQuery to wrap text within <b> tags and separate them with <br> tags

Upon receiving output in html string format from services, I am presented with the following: "<html>↵<h1>↵Example : ↵<br>Explanation↵</h1>↵<hr>↵<b>key1 : ABCD <br>key2 : 2016-10-18-18-38-29<br> ...

Error encountered in Intellij for Typescript interface: SyntaxError - Unexpected identifier

I am currently testing a basic interface with the following code: interface TestInterface { id: number; text: string; } const testInterfaceImplementation: TestInterface = { id: 1, text: 'sample text' }; console.log(testInterface ...

A step-by-step guide on incorporating universal CSRF tokens using JQuery AJAX

As part of my development work, I am in the process of creating jquery code that communicates with the server through ajax to input data into a database based on specific request parameters. My main concern at this point is the vulnerability to CSRF attac ...