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

120
Views
How to import a single thing from an array with objects in Javascript?

I'm building a search function in javascript.
I made an array in a separate file with objects.
I'm trying to display 1 thing from the object that's inside of the array.
I already defined the path to the image so the only thing I'm storing in the array is the name. But it is still showing me a broken picture
Does someone have an idea?
Thanks!
Code:

// the entire code: https://codepen.io/Yung_n-d/pen/mdwQGqX
var data = [

//The list
    {
        workshop: '3D Animatie',
        age: '',
        wsType: 'beeldkunst',
        wsLink: 'workshopPages/beeldkunst/3danimatie.php',
        level: '',
        photo: 'Beeldkunst.png',
    },  
    {
        workshop: 'Cartoon Tekenen',
        age: '',
        wsType: 'beeldkunst',
        wsLink: 'workshopPages/beeldkunst/cartoontekenen.php',
        level: '',
        photo: 'Beeldkunst.png',
    }]
            var output = '<div class="list-group">';
            $.each(data, function(key, val) {
                
                //If workshop matches search result, display
                if( ((val.workshop.search(caseExp) !== -1) || (val.wsType.search(caseExp) !== -1) ) 
                    && ( (val.age.search(ageValue) !== -1) && (val.wsType.search(typeValue) !== -1) && (val.level.search(levelValue) !== -1) ) ){
                    
                    
                    output += '<a href=' + val.wsLink + ' target="_blank" > <div class="list-group-item"><h4 class="list-group-item-heading">' + val.workshop + '</h4></a>'; 
                    output += '<p> Leeftijdsgroep: ' + val.age + '</p>' + '<p> Hoofdcategorie: ' + val.wsType + '</p>' + '<img src=\'media/fotos/gallery/\'' + val.photo + '></div>';
                    
                }
                
            });
            output += '</div>';
            $('#searchUpdate').html(output);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As @CBroe said in the comment, it was a typo when you build the image url. But you also have some tag mismatches in the code.

In a case like this it's much better to use string interpolation to build your HTML. You can do it for the whole item like this:

output += `<div class="list-group-item">
             <a href="${val.wsLink} target="_blank">         
               <h4 class="list-group-item-heading">${val.workshop}</h4>
             </a> 
             <p> Leeftijdsgroep: ${val.age}</p>
             <p> Hoofdcategorie: ${val.wsType}</p>
             <img src="media/fotos/gallery/${val.photo}">
           </div>`;

This makes the code much more readable and would help prevent typos like it had.

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!