What is the best way to combine a JSON response object with the variable J inside a for loop?

Received a JSON response as follows:

{
  "name" : "chanchal",
  "login3" : "1534165718",
  "login7" : "1534168971",
  "login6" : "1534168506",
  "login5" : "1534166215",
  "login9" : "1534170027",
  "login2" : "1534148039",
  "lastname" : "khandelwal",
  "login4" : "1534166200",
  "login10" : "1534147907",
  "login8" : "1534169966",
  "login1" : "1534147962",
  "id" : "1",
  "user_id" : "1002"
}

The code for the Script function is provided below:

function showHistory(usrId)
{
    $.ajax({
        type: "POST",
        url: loc+"AdminController/loginHistory",
        data: { usrId : usrId },
        success : function(data)
        {
            var obj = JSON.parse(data);
            var output="";
            console.log(data);
            output += '<div class="tabs">';
            output += '<ul class="tab-links"><li class="active"><a href="#name">'+obj.name+' '+obj.lastname+'</a></li></ul>';
            output += '<div class="tab-content">';
            output += '<table class="flat-table"><tbody><tr><th>No.</th><th>Login Time</th></tr>';              
            for(var j = 1 ; j<= 10 ; j++)
            {
                output += '<tr>';
                output += '<td>'+j+'</td>';
                output += '<td>'+obj.login+j+'</td>'; 
                output += '</tr>';  
            }

            output += '</tbody></table>';
            output += '</div></div>';
            $('#result_tbl').html(output);
        }
    });
}

Desired to display login times in a table by concatenating login with numbers from 1 to 10. Any suggestions on how to achieve this efficiently?

Answer №1

Modify:

output += '<td>'+obj.id+'</td>';

Into:

output += '<td>'+obj['id'+k]+'</td>';

So you can access properties id1, id2... id10

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

Is there a way to eliminate spaces from a string using react-native?

As a user of react-native, I've encountered an issue with inconsistent line breaks when inputting long strings on the screen. For example: https://i.sstatic.net/32RwW.jpg I aim to achieve consistent string wrapping as shown in the image below: htt ...

Safari displays the contents of JSON files instead of automatically downloading them

I am facing an issue with a JavaScript code that generates a link to download a JSON file. The link is structured like this: <a href="data:text/json;charset=utf-8,..." download="foo.json">download</a> While the link works perfectly in Chrome ...

Emphasize Expandable Sections based on Search Term

I have been working on developing an HTML page for my company that will showcase a list of contacts in an "experts list". Currently, the list is structured using collapsible DIVs nested within each other. Additionally, the HTML page features a search func ...

Compiling TypeScript files with an incorrect path when importing, appending "index" at the end of the @angular/material library

I'm currently working on creating a library to collect and distribute a series of Angular components across various projects, with a dependency on angular/material2. My objective is to eventually publish it on npm. However, I've encountered an i ...

What causes variations in versions displayed by Node.js?

What causes this error to appear when using node version v16.17.1? https://i.sstatic.net/yJdEx.png ERROR: npm is known not to run on Node.js v10.19.0 UP--- I had multiple versions of node installed, with the default being an older version. I was able t ...

Troubleshooting: Issues with URL redirection on localhost using Node.js

I've developed a service to verify if the user is logged in, but I'm encountering difficulties running the code on localhost. What could be causing this issue? The redirection isn't functioning as expected, should the code for redirecting t ...

State is not currently utilizing the variable

const DonorsTables = () =>{ const [search, setSearch] = useState(""); const [countries, setCountries] = useState([]); const [filteredcountries, setFilteredCountries] = useState([]); const getCountries = async () => { try { ...

Invoke a function from a popup window, then proceed to close the popup window and refresh the parent page

When a link in the parent window is clicked, it opens a child window. Now, when the save button is clicked in the child window, I need to trigger a Struts action, close the child window, and reload the parent window. function closeChildWindow(){ document. ...

Attain worldwide reach

I'm currently facing a Scope issue that has been quite challenging to resolve. The saying goes, "a picture is worth a thousand words," and in this case, it couldn't be more true. Whenever the OK or REJ buttons trigger the reject() function, passi ...

Is it possible to replicate a stale closure similar to React's useEffect hook without the use of the useEffect hook?

I have a good understanding of closures, but I am struggling to grasp how a stale closure can be created in React's useEffect without providing an exhaustive dependencies array. In order to explore this concept further, I am attempting to replicate a ...

Unveil concealed information within a freshly enlarged container

As I organize my content into an FAQ format, I want users to be able to click on a link and expand the section to reveal a list of items that can also be expanded individually. My goal is to have certain list items expand automatically when the FAQ section ...

How can I retrieve the POST values using xmlhttp?

This is an example of AJAX functionality. xmlhttp.send("fname=Henry&lname=Ford"); UPDATE I am looking to extract values from text/input fields after the equals sign. The current setup shows the values directly written out. How can I retrieve and dis ...

jQuery's draggable and resizable functionality with containment provisions is designed to

I'm struggling to create a resizable and draggable div using jQuery, but it's proving to be quite buggy in its implementation. Despite finding similar questions, I haven't come across a solution yet. How can I fix the containment bug when ...

What steps can be taken to prevent a Javascript Timeout Exception when attempting to launch a new browser window?

Recently, I have encountered an issue while running my test cases on a Linux server. Specifically, when trying to open a new window using Robot Framework, I consistently receive a Timeout Exception. This problem seems to be isolated to the server environm ...

What is causing .attr('title') to retrieve the title of the element before?

UPDATE Flexslider now includes several callback spaces for use, making this functionality possible. after: function(){}, //Callback: function(slider) - Fires after each slider animation completes Thank you to everyone who contributed! LIVE CO ...

Exploring the capabilities of the innovative Node's EventTarget class alongside ES6 modules

Exploring the new DOM-compatible EventTarget class introduced in Node.js 14.7.0 has caught my interest. I've noticed that I can only utilize it within a CommonJS module, and not an ES6 module. Here's an example: Executing node --expose-internal ...

What is the method for rendering an ejs template from express using fetch without the need to submit a form?

login.js file: const form = document.getElementById('loginForm'); form.addEventListener('submit',async(e)=>{ e.preventDefault(); return await fetch(`localhost:8000/route`, { method: "get", heade ...

discovering a new type of mutation through the use of Vuex

Vue Component computed: { score () { this.$store.commit('fetchCoordinates'); console.log(this.$store.getters.cordinate); } } store.js export default { state: { lat:'ok', }, getters:{ cordinate(state){ r ...

Update the controller variable value when there is a change in the isolate scope directive

When using two-way binding, represented by =, in a directive, it allows passing a controller's value into the directive. But how can one pass a change made in the isolated directive back to the controller? For instance, let's say there is a form ...