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

80
Views
looping over nested array to create elements with js/jquery

I come with a problem, I hope you can help me solve. I have already searched on this side quite a bit, but couldn't find an answer that solves my problem. I recently worked with looping over objects with jquery each and that worked fine. ATM I need to loop over entries of an array.

This is an example of how the array looks. It contains arrays, which themselves contain the src of an img and the title of the img.

var imgRef = [[http://127.0.0.1:5555/images/imgf0002.png, fig number 1],[http://127.0.0.1:5555/images/imgf0012.png, fig number 12]]

What I would like to do is to loop over the arrays and create a div for each entry. Inside the divs there should be an img containing the src of the other array and a p tag with the name. Then this will be appended to another element on the website.

<div>
  <img src="http://127.0.0.1:5555/images/imgf0002.png"> 
  <p>Fig number 1</p>
</div>
<div>
  <img src="http://127.0.0.1:5555/images/imgf0012.png"> 
  <p>Fig number 2</p>
</div>

I have started with this. Just to see whether I can loop over the entries, but it didn't work.

$.each(imgRef, function (index, value) {
    $('<div />', {
      'text': value
    }).appendTo('#localImg');
  });
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

    var imgRef = [['http://127.0.0.1:5555/images/imgf0002.png', 'fig number 1'],['http://127.0.0.1:5555/images/imgf0012.png', 'fig number 12']]
    $.each(imgRef, function(index, value){
        $('.localImg').append('<div><img src="'+value[0]+'"><p>'+value[1]+'</p></div>');
    });
about 4 years ago · Santiago Trujillo Report

0

you've got to iterate over the array with arr.forEach((elem) => {}) or other iterator methods. Since each of elements of the main array, is an array of two elements, you can use array destructuring and use [url, title] instead of elem in the iterator function. Then in the body create the html string from title and url and append it into your target using $.append method.

let theArray = [["url1","title1"],["url2","title2"] /*,...*/];
theArray.forEach(([url, title]) => {
    $('#localImg').append(`<div>
  <img src="${url}"> 
  <p>${title}</p>
</div>`);
});
about 4 years ago · Santiago Trujillo Report

0

var imgRef = [['http://127.0.0.1:5555/images/imgf0002.png', 'fig number 1'],['http://127.0.0.1:5555/images/imgf0012.png', 'fig number 12']]


$.each(imgRef, function (index, [src, text]) {
    // create the div element
    const div = $("<div></div>")
    // create the image and add src for it.
    const img = $('<img />', { 
      src,
     });
    // create the p element and add the text to it.
    const p = $('<p></p>', {
      text
    })
    // append both image and p in the div
    div.append([img, p])
    // then insert the block inside the localImage container.
    $('#localImg').append(div)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="localImg"></div>

about 4 years ago · Santiago Trujillo 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!