Tips for efficiently passing an array of functions to Object.assign and applying the same argument to each one

Here is an example of my code:

return Object.assign({}, generators(config));

When the generators variable is a single function, everything works fine. However, if it's an array of functions (Function[]) I want to execute all of them with the config argument passed to each.

Query:: How can I pass multiple functions to Object.assign and supply each with the config argument?

Answer №1

let myVar = Object.assign({}, myFunc([{
  operation: task1,
  setting: {x:1}
}, {
  operation: task2,
  setting: {y:2}
}]))


function task1 (setting) {
  return setting;
}

function task2 (setting) {
  return setting;
}

function myFunc (actionList) {
  let newObject = {};
  for (let j = 0; j < actionList.length; j++) {
     Object.assign(newObject, actionList[j].operation(actionList[j].config))
  }

  return newObject;
}

console.log(myVar);

Answer №2

Here is the solution:

  const temporary = generators.map(g => g(configuration));
  return Object.assign({}, ...generators.map(g => g(configuration)));

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

Attempting to incorporate Google charts into a div using jQuery's load function

Trying to incorporate a basic Google chart using jQuery .load functionality to transfer the chart into another webpage with a specific containing DIV: <html> <head> <script type="text/javascript" src="https://www.google.com/jsapi">< ...

Learn the process of updating an Array with a list of values based on checkbox selection and dynamic filtering using AngularJS

$scope.selectObjectType = function () { $scope.selected = []; // reset previous selection $scope.model.allItemsSelected = true; // all are selected by default unless... // If any object type is not checked, then uncheck the "allItemsSelected" ...

Issue when transferring properties of a component to a component constructed Using LoadingButton MUI

Check out my new component created with the LoadingButton MUI: view image description here I'm having issues passing props to my component: view image description here Dealing with a TypeScript problem here: view image description here I can resolv ...

I am experiencing issues with my for loop not functioning correctly within my On-Click Event in Javascript

Hello, I'm facing a bit of an issue with a for loop inside an On-Click Event. It seems like the loop is only showing me the last value from the array. Can someone please lend a hand here? Below is the code snippet where I have an array with 10 values ...

Tips for making multiple views function with angularjs' ui-router

I attempted to follow the instructions provided here but I am unable to make it work. https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views I made sure that my bootstrap grid divs were functioning correctly by placing them all in index.html an ...

Do not directly change a prop's value as it will be replaced when the parent component re-renders. Use v-form instead

I'm currently in the process of developing an app using nuxt along with vuetify 2.x, and I keep encountering a specific error message: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Inste ...

Turning simple text into JSON format

Beginner JS coder here, currently developing a program that pings IoT devices on a network and outputs a true or false value based on the success of the ping. Everything is functioning well so far, but I'm now attempting to extract specific propertie ...

Implementing tooltips using JavaScript within Bootstrap 5

Is there a way to add both a tooltip and popover to the same element in Bootstrap v5 via Javascript? I have attempted to enable both of them using JS as it seems like the best approach. The tooltip that was added through html is functioning correctly: ...

Tips for customizing the main select all checkbox in Material-UI React data grid

Utilizing a data grid with multiple selection in Material UI React, I have styled the headings with a dark background color and light text color. To maintain consistency, I also want to apply the same styling to the select all checkbox at the top. Althou ...

What is the reason for setting the initial value of a Vue computed property to a Javascript object property?

Can someone please explain why the Javascript object property, data.test, always holds the initial value of the computed property, square? Even after submitting a number in the input field, data.test remains set to 4. On the other hand, the computed proper ...

Solving Typing Problems in React TypeScript with "mui-icons" Props

I have a small compost project with only an App.JSX file that is functioning perfectly, but now I need to convert it to App.TSX. After changing the extension to .TSX, I encountered two errors that I'm unsure how to resolve. function MyComponentWithI ...

The React Component has been displayed on the screen a total of 5 times within the Ionic

While working on my app that includes a dashboard to display data, I noticed in Chrome's console that the render method is being called 5 times. This results in an annoying bouncing effect when the app page is re-rendered. How can I prevent the render ...

A script to continuously execute a form action in a loop

As someone transitioning from software development to web development, I find the study of web development quite confusing. Please bear with me as I ask this newbie question. My goal is to test every domain within my array (dMains) so that each domain can ...

Tips for updating form tag details within the same blade file upon reloading

I have set up a payment page that displays the total amount to be paid. To facilitate payments through a 3rd party gateway, I have utilized a form tag on the page. Now, I am looking to integrate a promo code feature on this payment page so that the total a ...

Unable to get AlertController to function properly within FCMPlugin in Ionic 2

Trying to implement an alert using AlertController within an Ionic app inside the FCMPlugin.onNotification() function, but encountering issues where the alert controller is not being created. It seems that the method halts execution after the code for crea ...

Unable to assign a class to the menu option

I'm encountering an issue with the following code: JavaScript $(document).ready(function() { var url = window.location; var element = $('#nav1 li a').filter(function() { return this.href == url || url.href.indexOf(this.href) == 0; ...

Having trouble creating a text channel in discord.js

Today I successfully programmed a bot to respond to the "!new" command, but encountered an unexpected issue. The current functionality of the bot creates a channel named "support-1" when prompted with the "!new" command. However, every subsequent use of th ...

Writing to the database right before exiting the webpage

A technique for detecting when a user leaves a page. const captureLeavingPage = () => { window.onbeforeunload = function (evt) { var message = "Are you sure you want to leave?"; if (typeof evt == 'undefined') { evt = window.ev ...

Creating a dynamic Three.js scene based on a variable

Having limited knowledge in html/javascript, I decided to start with three.js. My scene relies on a parameter that users can manipulate. Below is a simple example of the scene setup. It showcases a parametric surface that adjusts based on a user-controlle ...

Tips for accessing and displaying JSON data using jQuery

Data in JSON format: [ { "ID":"25", "Serial":"1", "Purchase_id":"8", "Item":"23", "Unit":"1", "HSN":"84212120", "Quantity":"10", "Purchase_rate":"100", ...