Include an index within the array

I need to incorporate an index in my array:

Here is the loop I am using:

for(j=0; j< data.data.tickets.length; j++) {
  var created_at = data.data.tickets[j].created_at;
  var tickettitle = data.data.tickets[j].subject;
  cleartab[requesterid]['tickets'] = [{"created":created_at, "titre":tickettitle}];   
}

The resulting array is presented here: https://i.sstatic.net/SlvRF.png

The issue lies in the fact that the key in tickets[] keeps getting overwritten, preventing proper iteration. My users have multiple tickets, each represented by an index with a date and title.

Answer №1

The issue lies in the fact that you are replacing the tickets array with each iteration of your for loop. To resolve this, consider the following solution:


for(k=0; k< data.data.tickets.length ;k++){
    var created_date = data.data.tickets[k].created_at;
    var ticket_subject = data.data.tickets[k].subject;
    
    // Ensure 'tickets' remains an array
    var ticketsArray = cleartab[requesterid]['tickets'] || [];
    cleartab[requesterid]['tickets'] = ticketsArray.concat([{"created":created_date,"title":ticket_subject}]);
}

Answer №2

This method is sure to be effective for you...

<script>
var customtab ={entries :[]};
$.each(details.record.entries, function(index,value) {
    var date_created = value.date_created;
    var entry_title = value.title;
    customtab.entries.push({"date":date_created, "title":entry_title});
});
console.log(customtab) 
</script>

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

intl-tel-input's getExtension function is returning a value of 'null'

I've been attempting to retrieve the extension of the mobile number that has been input. All other variables are functioning correctly, but the extension variable is returning a null value. It appears that it is sending a null value to the POST method ...

Obtaining the innerHTML value using JavaScript in an Asp.net Core 5 View

Apologies for any inaccuracies in my English writing. In the Asp.Net Core project view, I am trying to use JavaScript to capture multiple Span tags within innerHTML represented by a loop and display their sum in another tag. However, I am only able to ret ...

Sending an array to unmanaged code and receiving it back without the need for duplication

Currently, I am working on creating a C# wrapper for my C++ library, and I am facing a challenge with passing arrays to unmanaged code and reading the results back in .NET. My goal is to have a wrapper function that can take an array of floats as input (m ...

Is it acceptable to utilize $$updated while expanding a firebase factory in order to execute a getTotal() method similar to the one demonstrated in the example

Is there a way for me to retrieve the data returned by $$updated or have $$updated trigger a function that I can then receive every time a task is marked as completed? By the end of the day, I need to maintain a count of how many tasks users have finished ...

Adjusting the Materui LinearProgressWithLabel progress value to reflect a custom value

Is it possible to update the progress value of Material UI's LinearProgressWithLabel component with a custom value instead of the default one? I am trying to achieve this by obtaining my desired value from the upload progress in the Axios.post method, ...

I'm baffled by the fact that my routes appear to be non-existent. I cannot comprehend the reason behind this issue

I'm fairly new to web development, and I've been struggling with this issue for the past hour. Even after simplifying my code to the bare minimum, it's still not working. Here's what I have so far: app.js: const express = require(&apo ...

Using setTimeout or setInterval for polling in JavaScript can cause the browser to freeze due to its asynchronous

In order to receive newly created data records, I have developed a polling script. My goal is to schedule the call to happen every N seconds. I experimented with both setTimeout() and setInterval() functions to run the polling task asynchronously. However ...

Create a new JSON array by filtering this JSON array based on a specific date range

Having a json array that contains the following keys and values: { "results": [ { "date": "2013-15-5", "position": "23", "race" : "2" }, { "date": "2013-15-6", "positio ...

Arrange array items according to the values of two attributes

Within my array of N objects, each object is structured with the following properties: { id: 'an id', description: 'a description', isUser: true/false } My goal is to sort this array based on two criteria: Firstly, any object w ...

Notification that appears when accessed on a mobile device

I have encountered an issue with my website where mobile users are being continuously redirected to the mobile site due to the hosting of the sites in different places preventing the use of cookies to remember the redirection. As a result, a loop occurs. ...

Preloading images before loading a div using JavaScript

Can you walk me through implementing object first and then mergeObject in JavaScript? I have an interesting scenario where I need to display the original list followed by a merged list after a short delay. How can I achieve this using JavaScript? Specific ...

"React - encountering issues with state being undefined when passing child state up through parent components

I am currently navigating the world of react and have encountered a hurdle. I find myself facing difficulties in updating the parent component based on changes in the child state. I was able to pass the child state to the parent by linking the child's ...

Exploring Node.js Express: Understanding the Difference Between Modules and Middleware

I am working on an express rest api and need to create a PDF document with a link to download it. I have successfully generated the PDF, but now I want to create a route specifically for returning the PDF link. In addition, I also need other routes that ...

What are the steps for scheduling asynchronous tasks using Promises in Node.js?

As a beginner in Javascript, I am struggling to understand how to execute my functions sequentially. My goal is to use Promises to accomplish this task. I am currently following a tutorial on creating a chat bot for Facebook Messenger. Essentially, I want ...

Incorporating new information into an array following the utilization of its current data

Having trouble with creating and retrieving data in an array from a MySQL query? Look no further! Take a look at the code snippet I've been working on: $vars['items'] = $this->_query('SELECT customers.*, sites.site_name as `default ...

Struggling to get troika-three-text installed via npm?

I'm having trouble integrating Troika with my three-js scene. While others seem to have no issue, I am struggling to call the module and encountering problems with references. It's frustrating because I can't seem to find any resources to he ...

issue with transparent html5

Here is the code snippet I am struggling with: function clear(){ context2D.clearRect(0, 0, canvas.width, canvas.height); } function drawCharacterRight(){ clear(); context2D.setTransform(1, 0.30, 1, -0.30, 10, 380);//having issues here c ...

Restrict the quantity of user responses with JavaScript

For this questionnaire, users are limited to selecting and ranking only 3 out of the 5 listed Social Apps. I am currently using questback to build the survey, but I require assistance in implementing a JavaScript condition. <table> <tr> & ...

Can you merge the values between two arrays of objects by counting them and combining them into a single array?

I'm faced with an array conundrum categories: [ { name: 'category1', items: 0, }, { name: 'category2', items: 0, }, { name: 'category3', items: 0, }, ] And then there's this separate ar ...

Manipulating CSS Class Properties Using JavaScript

In my JavaScript application, there is a functionality that loads a list of items for users to click and view detailed information in a separate div on the page. Users should be able to interact with and make modifications to these individual item overview ...