Mapping an object containing arrays using Javascript

When it comes to mapping an object of arrays, my goal is to display the first row of content within a div after the mapping process. I am working with an object that contains multiple arrays from the database, but I am only focusing on mapping 2 out of the 4 arrays present.

The challenge lies in utilizing the mapped arrays to retrieve all corresponding data and present it within a div. The user should then be able to navigate through this data by clicking an up or down arrow. However, I am currently facing issues in displaying the next or previous set of data from the object. While the click function is correctly configured (and has been successful with test data), I suspect that the problem may lie in how I am mapping the arrays.

This is the original object retrieved from the database:

object: {
    PageNum: [array of items],
    RowNum: [array of items],
    CustomerName: [array of items],
    FacilityName: [array of items]
}

Here is how I am mapping the arrays:

var delDS = [{
    pageNum : delPageData["PageNum"],
    rowNum : delPageData["RowNum"]
}];

var delMappedArray = delDS.map(function(obj) {
    var rObj = {};
    rObj[obj.pageNum] = obj.rowNum;
    return rObj;
});

As a result, the mapped data structure looks like this:

[object]
   0: Object
     2,2,4,4,6: Array(5)
      0: "24"
      1: "26"
      2: "2"
      3: "4"
      4: "10"
     length: 5

Answer №1

Consider using the following approach:

// Transforming the data
const mappedData = originalData.pageNumbers.map((num, index) => ({
    pageNum: num,
    rowNum: originalData["RowNum"][index],
    customerName: originalData["CustomerName"][index],
    facilityName: originalData["FacilityName"][index]
}));

// Sorting the transformed data
mappedData.sort(function(a, b) {
    if (a.pageNum === b.pageNum) {
        return a.rowNum - b.rowNum;
    } else {
        return a.pageNum - b.pageNum;
    }
});

// Adding an index property for future ordering
mappedData.forEach((obj, index) => {
    obj.index = index;
});

This code snippet maps the original data array to create a new array of objects, sorts them based on page numbers and row numbers, and adds an index property to each object for reference purposes in your code.

Feel free to adapt this solution to suit your specific requirements!

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

What could be the reason behind the application experiencing crashes while utilizing express-rate-limit?

I am utilizing version 6.0.1 of the express-rate-limit package to control the number of request hits, and I referenced the express-rate-limit documentation available at https://www.npmjs.com/package/express-rate-limit However, I am encountering a crash in ...

Can WebAssembly code be executed asynchronously?

I've created a C function that can be run from Angular/TypeScript/JavaScript using WebAssembly. testWebAssembly() { Module.ccall("aCFunction", null, [], []); // takes a few seconds to finish } This particular function involves complex mathematic ...

Exploring the process of implementing inheritance in TypeScript from a JavaScript class

I am using a JavaScript module to extend a Class for a Custom extended Class. I have written my Custom Class in TypeScript, but I encountered the following error messages: Property 'jsFunc' does not exist on type 'tsClass'.ts(2339) I ...

Having two ng-click events and two distinct classes in an ng-repeat based on the selected option

There are two buttons in my code that should remove a div within an ng-repeat loop. Depending on which button is clicked, a custom CSS class should be added to the effect. The CSS changes based on the option selected. When I click on a button, either &apo ...

Tips for maintaining pagination state in datatable when making ajax calls

Summary of my question How can I keep the datatable on the same page after updating a row using ajax in my unique way? Challenges I'm facing When a user clicks a button in the table to update a specific row and I call the fn_GetData() function. Af ...

Transforming a List of Items into a Hierarchical Tree Structure

Seeking assistance with constructing a hierarchical tree structure from a flat list that contains categories and names. Various approaches have been attempted, including the function presented below. The original flat list looks as follows: var input = [ ...

Tips for showcasing a table generated from various input types on a separate page after pressing the submit button

After creating two JavaScript functions, I am eager to utilize both of them upon pressing the submit button on my form. The first function is already integrated into the submit button and activates a paragraph display upon submission. Now, I intend to sh ...

Custom attribute in ReactJS component - I am not defined in my custom attribute in React component

I am currently working with ReactJS and I am facing an issue regarding accessing a custom attribute -name- of my ReactJS component. Despite trying to use e.target.name, it keeps returning "undefined" on my console. I cannot seem to figure out why this is ...

Initiate the Selenium test once the JavaScript has completed its execution

When using Selenium, I am encountering issues with testing my page before all JavaScript initialization is complete. This can lead to a race condition and elements not being properly initialized. Is there a way to determine when all JavaScript rendering ha ...

In what way does the Node console showcase decimal numbers in floating point format?

While I understand the challenges of representing base 2 numbers in base 10 due to rounding issues in programming languages, my experience with the NodeJs console has left me puzzled. It is a known fact that base 2 numbers cannot perfectly represent 0.1 in ...

submit messages that have been sent before in the group chat

Currently, I am employed at a messaging application where I aim to save messages in an object structure like this : { 'room1': ['msg1', 'msg2', ...] 'room2': ['msg3', 'msg4', ...] ... } To ...

Locate the nearest value to a fixed number within a multi-dimensional array using Matlab

As I work with a matrix B B(:,:,1) = 2 8 0 5 B(:,:,2) = 1 3 7 9 My aim is to determine the index of a value close, for instance, to 2.9. I experimented with the following code: [r,c,v] = ind2sub( ...

What is the best way to delete markers from a leaflet map?

I need to remove markers from my map. I am looking to create a function that will specifically clear a marker based on its ID. I am utilizing Leaflet for the map implementation. Here is my function: public clearMarkers(): void { for (var id in this. ...

Converting a floating point number to a 4-byte hex string in JavaScript and reversing the process

I have received a hexadecimal data from the server side that is supposed to be in float format. I am trying to convert these hexadecimals into floats using JavaScript, but so far I have been unable to find a suitable method. Can anyone provide assistance ...

Webpack converts 'import' statements to 'require'

I'm currently in the process of compiling my nodeJS project using webpack. Everything seems to be working correctly after compilation, but I've noticed that the imports are being changed to requires. This causes an error when trying to run index. ...

Looking to dynamically display users added to an array in a table using Angular and JavaScript?

As a newcomer to javascript and angularjs, I recently attempted to build a table that would display all users in an array dynamically as new users are added through a form. However, each time I run my code, I encounter the error message "Fill out the entir ...

Insert several rows in Google Sheets using the npm package

Currently, I am utilizing the npm package google-spreadsheet for managing Google spreadsheets. However, I have not come across a method within this npm package that allows for adding multiple rows at once, similar to what the Google Sheets API offers. I h ...

Securing Strings with XOR Encryption and BSAFE Hashing (Similar to OpenSSL Library)

I am facing a challenge with calculating the SHA-2 hash of an XOR encrypted string using RSA BSafe library. The XOR function I am currently using is as follows: for (int i = 0; i < strlen(combined); i++) { encrypted[i] = (combined[i] ^ authkey[i%st ...

When using Next.js and Express.js together, CORS error may occur, causing API queries to only function properly during build

I am currently working on a project that involves using Next.js for the front-end and Express.js for the back-end. Front-end Setup The 'pages' directory contains an 'index.js' file where I have implemented the following code snippet: ...

transferring information between two ajax calls upon completion of the first ajax request

Attempting to pass data to jvectormap from an ajax call, using a when function to ensure the code runs after the ajax call has finished loading. The issue I'm facing is that the map appears empty and I encounter an error. Uncaught ReferenceError: ...