0

I have this code that shows all the images that are in a json file. Any way to show only 1 image and randomly with the ones in the .json?

I have this code:

 $(document).ready(function() {
   
        $.getJSON('https://res.cloudinary.com/dkx20emez/image/list/dom.json', function(emp) {
          //the attribute resource is an array so you have to loop each element in it
      emp.resources.forEach(function(element){   
            var publicid = element.public_id;
            var format = element.format;
            var type = element.type;
            var version = element.version;
            
            $('#display').append('<img class="wrapper" src="https://res.cloudinary.com/dkx20emez/image/'+ type +'/v'+version +'/'+publicid+'.'+format +'">');
            
            
        
          });
        });
   
   
});
 **css**
body { padding: 50px; }

.wrapper {
  width: 200px;
  height: auto;
  padding: 10px;
  background: #eee;
}

.random-faces {
  width: 100%;
  max-width: 200px;
  height: 0;
  padding-bottom: 100%;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
}
          //css

<html><head>    <script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
    </script></head>

<body>

    <div class="wrapper" id="display" style="background-color:#39B54A;"></div>
    </body>
  </html>

I thank you for your answers

  • Hi, I want to display a single random image instead of the currently displayed list – María perez Kusky Feb 02 '22 at 00:11
  • You know your array, you just need to generate a random number to use as an index. Google Math.random() and use `emp.resources.length` as the limiter to pick a random number – Kinglish Feb 02 '22 at 00:14

1 Answers1

0

You can just generate a random integer between 0 and the length of the returned array -1. I have included an easy method that performs this from this post:

Generate random number between two numbers in JavaScript

Then you can use that random integer as the index of emp.resources to get a random image.

See snippet below:

(I've forgotten most of the jQuery that I once knew, so there is Vanilla JS mixed in)

// Custom function to get a random integer between two numbers
const randomIntFromInterval = (min, max) => Math.floor(Math.random() * (max - min + 1) + min);

$(document).ready(function() {

  $.getJSON('https://res.cloudinary.com/dkx20emez/image/list/dom.json', function(emp) {

    //Get a random number between 0 and the length of the emp.resources array - 1
    var index = randomIntFromInterval(0, emp.resources.length - 1)

    // Then just use that random number as the index of the array
    var element = emp.resources[index]
    
    var publicid = element.public_id;
    var format = element.format;
    var type = element.type;
    var version = element.version;

    //Create an img element
    const img = document.createElement(`img`);

    //Add in the class to the img
    img.classList.add(`wrapper`);

    //Add in the source to the img
    img.src = `https://res.cloudinary.com/dkx20emez/image/${type}/v${version}/${publicid}.${format}`;

    $('#display').append(img);
  });
});
body {
  padding: 5px;
}

.wrapper {
  width: 200px;
  height: auto;
  padding: 10px;
  background: #eee;
}

.random-faces {
  width: 100%;
  max-width: 200px;
  height: 0;
  padding-bottom: 100%;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: cover;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="wrapper" id="display" style="background-color:#39B54A;"></div>
Steve
  • 878
  • 1
  • 5
  • 9