What is the best way to utilize JavaScript methods for retrieving dynamic data?

My variable needs to return data based on an array of objects that contain dynamic dummy values.

There are 3 sets of dummies structured as follows -->


Example:

  • dummy1_A, dummy1_B, dummy1_C, ...
  • dummy2_A, dummy2_B, dummy2_C, ...
  • dummy3_A, dummy3_B, dummy3_C, ...

When returning the data, I want to set 'fieldName' and 'text'. This is the code I am using with JSON.stringify to display the desired data

I am utilizing the map method as well

let a1 = [
  {
    dummy1_A: 0.5526714707565221,
    dummy2_A: 0.5526714707565223,
    dummy3_A: 0.5526714707565224,
    dummy1_B: 0.5028423429150607,
    dummy2_B: 0.5028423429150605,
    dummy3_B: 0.5028423429150604,

  },
  {
    dummy1_A: 0.542947572819916,
    dummy2_A: 0.4965857885945633,
    dummy3_A: 0.4965857885945677,
    dummy1_B: 0.4470431086251489,
    dummy2_B: 0.3785646261205342,
    dummy3_B: 0.3785646261205345,
  },
];

let a2 = a1.map((x, i) => {
  let seqStr = String(a1.entries); // Assistance needed here
  return {
    text: seqStr,
    fieldName: seqStr,
  };
});

// output
[
  { text: 'dummy1_A', fieldName: 'A' },
  { text: 'dummy2_A', fieldName: 'A' },
  { text: 'dummy1_B', fieldName: 'B' },
  { text: 'dummy2_B', fieldName: 'B' },
];

We could also use forEach but it requires more logic

a1.forEach(obj => {
  const key = Object.keys(obj)[0];
  const newKey = key[key.length - 1];
  obj[newKey] = obj[key];
  delete obj[key];
});

After using console.log(JSON.stringify(a2)); I didn't get the expected result despite using map

Answer №1

const generateNewHeaders = inputArray => {
  return inputArray.map(item => {
    return {
      text: item,
      fieldName: 'dummy1_' + item,
      width: 110
    };
  });
};

Utilize the above function to create two additional arrays with lengths of 2 and 3 respectively.

Next, merge all the generated arrays into a single array using either the concat method or the spread operator.

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

How to Send a Two-Dimensional Dynamic Array to a Function in C++

I need to pass a 2D dynamic array to a function. Can someone guide me on how to accomplish that? int ** board; board = new int*[boardsize]; //creating a multi-dimensional dynamic array for(int i = 0; i < boardsize; i++) ...

Guide to dynamically generating Angular watchers within a loop

I'm looking to dynamically create angular watches on one or more model attributes. I attempted the following approach: var fields = ['foo', 'bar']; for (var i=0, n=fields.length; i<n; i++) { $scope.$watch('vm.model.&ap ...

route in mean stack isn't providing a response

I am having trouble creating a controller for /projects that should return all the data in the 'work' collection. The call is completing successfully with a status code of 200, but it is only returning an empty array or 'test:test' if u ...

Error in GLSL file: "Module parsing error: Unexpected symbol"

I'm currently working on creating a custom image transition using a shader, and I require a fragment.glsl file for this purpose. However, when I try to import this file into my .js file, I encounter the following error: Compiled with problems: ERROR ...

TS7006: Argument 'duplicate' is assumed to have an 'any' data type

I am working on a function that will create a button to copy the content of a variable into the clipboard using TypeScript. Below is my attempted code: const [copySuccess, setCopySuccess] = useState(''); const copyToClipBoard = async copyMe => ...

Having trouble with jQuery live clock freezing your browser?

I recently created a clock script and utilized setInterval to keep it running smoothly. However, I've encountered an issue where my browser freezes after a short period of time. Unfortunately, I'm unsure how to troubleshoot this problem on my own ...

Printing paths of nested options is incomplete

Is there a way to display the full path of a select with nested options in angular? When I click on an option, currently it only shows the option value. <select> <optgroup label="A"> <option>1</option> <optio ...

What are the steps to seamlessly incorporate and set up Node.js into a web application powered by CodeIgniter that is currently hosted on

After successfully deploying my Codeigniter based application to the online server, I now desire to incorporate instant messaging functionality using socket.io. This can be achieved by installing node.js. Can anyone provide guidance on how to install nod ...

Updating Mysql through REST API/JWT using PUT method is not possible

I have been attempting to send an update request using Jwt (Tokens) and Node.Js with a backend in mysql. While Postman confirms that the record has been successfully updated, I am unable to locate where the actual update occurred. No changes seem to be ref ...

How to implement horizontal scrolling in an Ionic/AngularJS app

Struggling to implement a scroll navigation with ionic, but encountering issues with navigation and styling. Seeking guidance on how to achieve the desired outcome: Desired navigation look: https://i.sstatic.net/vQ2CS.png Current implementation showcases ...

Create a distinct timer for every item in the ngFor loop

Utilizing angular, a custom stopwatch has been created. The challenge lies in managing unique timers for each ngFor item despite having start/end buttons for each item. https://i.sstatic.net/c4oM8.png In the provided image, distinct start/end buttons are ...

Reply in Loopback on remote method beforeRemote hook is to be sent

In my application, I am utilizing loopback v3. The specific use case I am tackling involves validating the presence of a token in the request header. If the token is invalid, I need to send an appropriate message in a standardized format. My approach has b ...

unable to send array in cookie through express isn't functioning

Currently, I am in the midst of a project that requires me to provide the user with an array. To achieve this, I have been attempting to utilize the res.cookie() function. However, each time I try to pass an array as cookie data, the browser interprets it ...

``I just retrieved data from two separate Mongoose documents in the database

I'm currently working on my first app using mongoose database. I'm facing an issue with writing an express API call that should return an object containing data from two documents. Every time I access the /data route, I receive an empty array as ...

Encountered an error: "switch/mergeAll/flatten is not a valid function" when working with the http driver

As I delve into learning CycleJS, one thing that has caught my attention is the usage of Cycle's HTTP Driver. It seems that in order to reach the stream level, merging the response stream stream with RxJS switch/mergeAll is essential. However, when at ...

Optimizing CSS With jQuery During Browser Resize

I am currently facing an issue with recalculating the height of the li element during window resizing or scrolling. Strangely, on page load, the height is no longer being re-calculated and set to the ul's child height. Here is the code I have written ...

Navigating to a specific element using an href link causes issues when Bootstrap modals are

I wrote some code that allows for smooth scrolling to specific elements when their ID is entered in hrefs (check out the demo here): $('a[href*=#]:not([href=#])').click(function () { if (location.pathname.replace(/^\//, '&a ...

Curious about learning the Xpath?

This is the HTML content <dd id="_offers2" itemprop="offers" itemscope="" itemtype="http://schema.org/Offer" class="wholesale nowrap "> <span itemprop="price" class="Hover Hover Hover">$46.29</span> / each <meta itempr ...

Switching Icon in Vuetify Navigation Drawer Mini Variant upon Click Event

UPDATE Here's the solution I discovered: <v-icon> {{ mini ? 'mdi-chevron-right' : 'mdi-chevron-left' }} </v-icon> Is it feasible to modify the icon when toggling between navigation drawer variants? The default varia ...

Is there a way to identify which paragraph element was clicked and retrieve its innerHTML content using JavaScript?

Hi there! I'm facing an issue where I need my webpage to identify which paragraph was clicked, retrieve its inner text, and then adjust the size of an image accordingly. You can check it out here: http://jsfiddle.net/YgL5Z/ Here is a snippet of my HT ...