Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

288
Views
How to scan an array of objects inside other objects

Everybody, good afternoon, everybody,

I’m trying to access the attributes of a javascript object array via ajax, but I’m not succeeding, can anyone see where I’m going wrong?

enter image description here

My ajax call is like this:

$("#cbx1").on("change", function() {

var checkbox = $("#cbx1").val();
console.log(checkbox);
if(checkbox == "TF") {
    var url = "/listar-perguntas?area-tematica=" + $(this).val() + "&montar-navegadores=true";
    console.log("bateu aqui 1"+url);
} else {
    var url = "/listar-perguntas?area-tematica=" + $(this).val();
    console.log("bateu aqui 2"+url);
} 

$.ajax({
    type:'GET',
    data : "",
    url: url,
    contentType: "application/json; charset=utf-8",
    success: function(data) {
        console.log(data);
        let text = "<input>" 
        for (let x in data) {
            text += "<label>" + data[x] + "</label>"
        }
        text += "</input>"
        document.getElementById("temaFederal").innerHTML = text;
    },
    error: function(data){
        console.log(data.statusText);
    }
});

});

In my html it looks like this:

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have to first search for the object in which your label is stored. That is on the 10th position in the list. If the element is gonna be always on the last position you can simply do data[data.length -1] and that will select the last object on your list on root level. Once you have selected the object then you’ll need to do data[data.length - 1]?.teamnavigator?.bucket and that will give you the list on which your labels are stored. You can now iterate over this list.

Other wise you may search for the element first and then get your bucket list like this

const data = [
    { pergunta: 'lorem ipsum', guid: '1111'},
    { pergunta: 'lorem ipsum', guid: '1111'},
    { pergunta: 'lorem ipsum', guid: '1111'},
    { pergunta: 'lorem ipsum', guid: '1111'},
    { pergunta: 'lorem ipsum', guid: '1111'},
    { 
        areatematicanavigator: {},
        temanavigator: {
            bucket: [
                {
                    key: 'key',
                    label: 'label',
                },
                {
                    key: 'key',
                    label: 'label',
                }
            ]
        }
        
    }
]

const objectOfInterest = data.find(item => 'temanavigator' in item)

const buckets = objectOfInterest.temanavigator.bucket
for (let index in buckets) {
    console.log(buckets[index].label);
}

Edit: This is my first reply on stackoverflow. Pardon me for my silly mistakes.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!