Circularly looping through JSON data from an external file in Javascript

I am currently attempting to display JSON file data in a circular manner using JavaScript, one by one. However, I am having difficulty achieving this. Currently, I can either show all the data at once or just the first data using the JavaScript Slice function.

How can I loop through the data circularly and display one item at a time? Below is my code snippet. At the moment, this code limits the display to only 1 item. If I remove the slice function, I can see the entire dataset.

$(document).ready(function(e) {
//jobs('experiment','assets/js/job.json'); 
$('.announce-section').addClass('zoom');
$.getJSON("assets/js/job.json", function( result ){
    $('#wrapper header').html(result.title);
    $('#wrapper h2').html(result.subtitle);
        var rlength = result.jobs.length;
        alert(rlength);
        $.each(result.jobs.slice(0,1), function(key, field){
            var jlength = result.jobs.length;
            var img = field.image;
            $('.pattren').append('<div class="data screen'+key+'"></div>');
            if(img !='')
                {
                    $('.pattren .data').append('<img src="assets/img/'+field.image+'" />');
                }
        $('.pattren .data').append('<ul><li>'+field.label_1+'<span>'+field.description_1+'</span></li><li>'+field.label_2+'<span>'+field.description_2+'</span></li><li>'+field.label_3+'<span>'+field.description_3+'</span></li><li>'+field.label_4+'<span>'+field.description_4+'</span></li></ul>');
        });
});
});

Answer №1

There are a couple of methods to achieve this:

  • Utilize only one container div and rearrange the fields inside, such as image and description.

  • Create individual div containers based on the data; for example, if there are four employees, generate four divs and cycle through them in a circular manner.

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

Ways to maintain the value of req.session using express-session

Check out these lines of code: const session = require('express-session'); const sessionConfig = { secret: 'somesecretkey', cookie: {secure: false}, resave: false, saveUninitialized: false, store: new mongostore({ mo ...

What is the most effective method to convert PHP data into JSON and present it in the jQuery success scenario?

In the realm of jQuery, I have this particular piece of code: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script> <script type="text/javascript" > $(function() { $("input[type ...

Error: ResizeObserver is not defined when using Gatsby with nivo charts

I incorporated @nivo/pie using "gatsby": "^3.13.0", but encountered an error during the gatsby build process. WebpackError: ReferenceError: ResizeObserver is not defined. The version of Nivo being used is "@nivo/pie": "^0 ...

"Utilize Meteor to transmit emails to internal email addresses within the intran

I have successfully developed a Meteor App to manage requests. However, I am facing an issue where I need emails with default text to be sent whenever a request is created or updated. The challenge lies in the fact that I am operating within an intranet e ...

Collapse the mobile nav bar upon clicking an item

I'm currently facing a challenge with the navigation bar on my event landing page. The issue is that when I open the navbar in mobile view, it does not collapse back after clicking on an item. Instead, it remains open and covers almost 3/4 of the scre ...

Generating a JavaScript array in PHP

As I work on developing a dynamic Google Geochart, one key aspect is creating an array to hold the data. The structure includes the country as the unique identifier and the color value to determine the map shading. arrayData = [['Country',' ...

Upload a user-sent image to a remote SFTP server for safekeeping

Can someone help me figure out how to upload a file to an SFTP remote server using ssh2-sftp-client? I am trying to retrieve the file from the user via a post request along with the destination. To process the file, I am utilizing multer. const Client = r ...

Is it more efficient to pass session variables through an ajax function, or should I access them directly within the script?

I am currently working on a page that utilizes an ajax function to retrieve updates from another page. The function requires the user's id, which is obtained from a session variable, to fetch any new updates. These updates are then displayed in a spec ...

Prevent Gson from inspecting annotations on classes

I am attempting to utilize Gson on Android for marshalling/unmarshalling JAXB annotated classes. Due to the absence of JAXB (XML) annotations on the Android device, it is crucial that the JSON library does not inspect annotations - otherwise, an error lik ...

AngularJS- Controllers encountering the issue of $scope being undefined after invoking a function

Currently, I am working on a project that involves creating a website with various links using AngularJS routing. One of these links should only be visible to administrators. User authentication and role information are stored in cookies. Here is the basic ...

Tips on displaying the menu only when scrolling through the webpage

Currently, my website has a right menu that causes overlap with the content on mobile and small devices. To address this issue, I am working on hiding the right menu when the page is stable. The plan is for the menu to appear as soon as the user starts sc ...

JavaScript Animation: Enhancing TextBox Borders

I'm exploring an idea for a loading animation, but I'm feeling a bit lost on where to begin. Basically, I have a text box that gets populated automatically with JavaScript on the page, however, it takes some time to load. I want to create somethi ...

Examining the process through which an element attains focus

Scenario I am working on a Backbone application that has an event listener set up for focus events on a textarea. Since Backbone relies on jQuery events, my main concern revolves around jQuery focus events. Inquiry Is there a method to determine how an e ...

How can I resolve the issue of <td> being repeatedly displayed five times instead of just twice in PHP?

Can someone assist me with fixing this for loop issue? I am trying to display controls next to each item in the row, but it is showing 5 sets of controls instead of just 2. <tbody> <?php //retrieve list of supplies $numOfRows = 0; $result = my ...

Identifying Mistakes during Promise Initialization

Looking for a more efficient way to work with Bluebird promises Promise.resolve() .then(function() {return new MyObject(data)}) .then.....etc .catch(function (e){ //handle it}) I am dealing with MyObject and data coming from an external sourc ...

"Encountering a problem with posting API requests using Express

I've encountered a problem where I keep receiving a 404 error when trying to post to a specific route. Here's the code snippet: app.js (code snippet for app.js) .... module.exports = app; api.js (code snippet for api.js) .... module.export ...

Submitting a form using an anchor tag in Angular 8: A step-by-step guide

I have a question about how to submit form data using hidden input fields when a user clicks on an <a> tag. <form action="/submit/form/link"> <input type="hidden" [attr.value]="orderNumber.id" /> <input type="hidden" [attr.value]= ...

Exploring the contents of an array in ReactJS

const rowData = this.state.market.map((market) => { console.log("details", market["info"]) { return { marketInfo: ( <div> {market && !!market["info"] ? ( <div> ...

Is there a way to modify the material of individual objects in THREE.js without creating separate Meshes?

We have developed a unique PCB Viewer using THREE.js, but now we want to enhance it with selection functionality. While the task itself isn't complex and I have managed to make it work, I am encountering performance challenges. Our goal is to allow us ...

JSON Serialization of numeric data types

It came to my attention that the website generates the same Base64 string for payloads containing numerical values written in different notations. An interesting example is the output for these two payloads: { "value": 0.000001 } { "val ...