Javascript calls the function several times with arguments

This is not entirely necessary, I'm just trying to simplify my code. This is what I have:

function fillWebsitePlaceFiller(number) { document.getElementById("placefillerWebsite" + number).innerHTML = placefillerWebsite; } fillWebsitePlaceFiller(1); fillWebsitePlaceFiller(2); fillWebsitePlaceFiller(3); fillWebsitePlaceFiller(4); fillWebsitePlaceFiller(5); fillWebsitePlaceFiller(6); fillWebsitePlaceFiller(7); 

Is there a way that I can call the function only once, and will go through 7 times with each argument?

+9
source share
6 answers

Method 1 - Iteration

 for (var i = 1; i < 8; i++) fillWebsitePlaceFilter(i); 

Method 2 - Recursion

 (function repeat(number) { fillWebsitePlaceFiller(number); if (number > 1) repeat(number - 1); })(7); 

Method 3 - Application Functor

 [1, 2, 3, 4, 5, 6, 7].forEach(fillWebsitePlaceFiller); 

Method 4 - Internal Iteration

 function fillWebsitePlaceFiller(times) { for (var number = 1; number <= times; number++) { document.getElementById("placefillerWebsite" + number).innerHTML = placefillerWebsite; } } 

Method 5 - Extend Function Behavior

 Function.prototype.sequence = function(from, to) { for (var i = from; i <= to; i++) this.call(null, i); }; fillWebsitePlaceFiller.sequence(1, 7); 

Method 6 - XPath (Warning: Unverified)

 var query = '//*[@id[starts-with(., "placefillerWebsite"]]'; var result = document.evaluate(query, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); while (var node = result.iterateNext()) node.innerHTML = placefillerWebsite; 

Method 7 - jQuery

 $('[id^="placefillerWebsite"]').html(placefillerWebsite) 

I recommend one of the methods where you do not assume that there are always seven.

+39
source

With ES6 it can be solved a little more elegantly:

 [...Array(7)].forEach((_, i) => fillWebsitePlaceFiller(i + 1)) 
+8
source

Simple loop!

 for (var i = 1; i <= 7; ++i) { fillWebsitePlaceFiller(i); } 

Or, as easy, modify fillWebsitePlaceFiller to make your for loop:

 function fillWebsitePlaceFiller() { for (var i = 1; i <= 7; ++i) { document.getElementById("placefillerWebsite" + i).innerHTML = placefillerWebsite; } } 
+4
source

Sort of

  function fillWebsitePlaceFiller(number) { for(i =0; i < number; i++) document.getElementById("placefillerWebsite" + i).innerHTML = placefillerWebsite; } 
+3
source

use loop

 function doTimes(number) { for (var i = 0; i < number; i++) { fillWebsitePlaceFiller(i); } } doTimes(7); 
+2
source

The best and easiest way to repeat a single function multiple times is to use qalllib

Pros - 1. Iterate synchronization / asynchrony in accordance with requirement 2. Violations without promises 3. Returns all results in one array, like Promise.all

Example: https://www.npmjs.com/package/qalllib

 npm i qalllib const qall = require('qalllib') function doSomething(params) { return new Promise((resolve,reject){ //operations }) } qall.qAsyncAll(doSomething, [arg1, arg2,arg3],3) .then((response)=>{ console.log(response) }) //catchBlock 

Explanation: -

 qall.qAsyncAll(functionName without brackets, arrayOfArguments, arrayOfArguments.length) 

Another example

example

 let _ = require('underscore') let response = ['some data','other data'] return Promise.all(_.map(response, function (data) { return functionName(data) })) .then((response)=>{ console.log(response) }) 

Result

 ['response1','response2'] 
0
source

Source: https://habr.com/ru/post/949345/


All Articles