Changing function name with array [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Could someone say how to make an array of function names that will change in while loop?
Example of what I want:
var funcNames = ['function1()', 'function2()', 'function3()','function4()','function5()'];
var i = 0;
while( i < funcNames.length ) {
function funcNames[i] {
alert("hello");
}
i++;
}
In the example, the code doesn't work. It only shows how the code should work.

You can think about create new dynamic function name in javascript? like below:
var funcNames = ['function1()', 'function2()', 'function3()','function4()','function5()'];
var i = 0;
var result = [];
while( i < funcNames.length ) {
var funcName = funcNames[i];
var func = new Function("return function " + funcName + "{ alert('hello')}");
result.push(func);
i++;
}
console.log(result);
var firstFunc = result[0]();
console.log(firstFunc()); // invoke one func here

Related

Multiple search creteTextFinder app scrip [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm currently working on a project where I send data to a google sheet through app script.
I need to change the value of some data and used createTextFinder.
Thanks to #Mimi in this thread. But the fact is that I need to replace more than one text.
I need to change the text when the data is appending.
thanks in advance
Here is my code
function addUser(sheet) {
var FILE = SpreadsheetApp.openById("XXXXXXXXXXXXX");
var sheet = FILE.getSheetByName("Sheet1");
var id = "True";
var name = "FALSE";
var t1 = createTextFinder("TRUE").replaceAllWith("YES")
var t2 = createTextFinder("FALSE").replaceAllWith("NO")
sheet.appendRow([id,name]).t1.t2;
From I need to change the text when the data is appending., in this case, how about the following modification?
From:
var t1 = createTextFinder("TRUE").replaceAllWith("YES")
var t2 = createTextFinder("FALSE").replaceAllWith("NO")
sheet.appendRow([id,name]).t1.t2;
To:
var values = [id, name].map(e => {
var temp = e.toUpperCase();
return temp == "TRUE" ? "YES" : temp == "FALSE" ? "NO" : e;
});
sheet.appendRow(values);
For example, when you are required to use TextFinder, how about the following modification?
From
var t1 = createTextFinder("TRUE").replaceAllWith("YES")
var t2 = createTextFinder("FALSE").replaceAllWith("NO")
sheet.appendRow([id,name]).t1.t2;
To
sheet.appendRow([id, name]);
sheet.createTextFinder("TRUE").replaceAllWith("YES");
sheet.createTextFinder("FALSE").replaceAllWith("NO");
or, when you want to use TextFinder to the appended row, you can also the following modified script.
sheet.appendRow([id, name]);
// SpreadsheetApp.flush(); // This might be not required to be used.
const range = sheet.getRange(sheet.getLastRow(), 1, 1, 2);
range.createTextFinder("TRUE").replaceAllWith("YES");
range.createTextFinder("FALSE").replaceAllWith("NO");
Multiple Replacement
function addUser() {
const txtA = ['txt1', 'txt2', 'text3'];
const rplA = ['rpl1', 'rpl2', 'repl3'];
var ss = SpreadsheetApp.openById("XXXXXXXXXXXXX");
var sh = ss.getSheetByName("Sheet1");
txtA.forEach((t, i) => {
let f = sh.createTextFinder(t).findAll();
if (f) {
f.forEach(r => { r.setValue(r.getValue().replace(txtA[i], rplA[i])); });
}
});
}

How to add an item in array to list of html? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
const toDoForm = document.querySelector(".todo__form"),
toDoInput = document.querySelector(".todo__input");
const toDoArray = [];
function addToDoList() {
toDoForm.addEventListener('submit', event => {
toDoArray.push(toDoInput.value);
event.preventDefault();
});
}
I made a code to put an input value to an array, and i want to put it in the list of html. How do i do that?
You can display the elements from your array dynamically by building a string and insert the string into the element.
const toDoArray = ["Max", "mortiz", "martin"];
function displayArrayInList(){
let output = `<ul>`
for(let i = 0; i < toDoArray.length; i++){
output += `<li>${toDoArray[i]}</li>`
}
output += `</ul>`
document.getElementById('displayer').innerHTML = output;
}
displayArrayInList();
<div id="displayer"></div>

Receive a String in this format: "1-10" and create an array with the amount of numbers in the range [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
How to Receive a String in this format: "1-10" and create an array with the amount of numbers in the range. Print the array to screen using a for loop.
I.E - "1-5" received so they array will be: {1,2,3,4,5}
create for workflow with vCenter orchestrator.
You can split the string into array and then iterate in a loop to get the iteration.
let str = "1-5";
str = str.split('-');
for(let i = parseInt(str[0]); i<=parseInt(str[1]); i++) {
console.log(i);
}
You may use some cool ES6:
Array.range = function(s){
const [start,end] = s.split("-");
return Array.from({length:start-end}).map((_,i)=>i+ +start);
};
Usable like this:
Array.range("1-10") //[1,2,3...]
var input = "1-10"; //SAMPE INPUT DATA.
var foo = input.split("-"); //PASRING INPUT DATA.
var answer = [];
for(var i = foo[0]; i<= foo[1]; i++){
answer.push(parseInt(i)); //MAKE AN ARRAY.
}
console.log(answer);

updating the dom with array in jquery [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Want to know how to update the dom with an array of list items, similar to:
var localPlaceholder = document.getElementById('localFiles');
for (i=0; i<entries.length; i++) {
var newList = document.createElement( "li" );
newList.nodeValue = entries[i].name;
localPlacholder.appendChild(newList);
}
but simplified in jquery using the append method.
Just map the array of entries into an array of jQuery objects and append them in one go:
var entries = [{name: "foo"},{name: "bar"}];
$('#localFiles').append($.map(entries, function(entry) {
return $('<li>', { text: entry.name });
}));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="localFiles" />
See also: jQuery.map()
Just enter the raw html as a string.
var localPlaceholder = $('#localFiles');
var numEntries = entries.length;
for (var i=0; i < numEntries; i++) {
localPlaceholder.append("<li>" + entries[i].name + "</li>");
}

how to sort the object array to map with respect to fields in object array using java script [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an object array like this
[
{bookName:"javascript",authorName:"john"},
{bookName:"java",authorName:"peter"},
{bookName:"j2ee",authorName:"john"},
{bookName:".net",authorName:"peter"},
{bookName:"oracle",authorName:"peter"}];
I tried the logics but i didnt get correctly.I want to sort out like this..
{
peter:{
[{bookName:"java",authorName:"peter"},
{bookName:".net",authorName:"peter"},
{bookName:"oracle",authorName:"peter"} ]
} ,
john :{
[{bookName:"j2ee",authorName:"john"},
{bookName:"javascript",authorName:"john"}]
}
}.
how can i do that.. Thanks in Advance ..
var obj={};
var objArr = given objectarray;
for(var i=0;i<objArr.length.i++ ){
objArr[objArr[i].authorName]=objArr[i];
}
and
var obj={};
var objArr = given objectarray;
for(var i=0;i<objArr.length.i++ ){
var arr=[];
objArr[objArr[i].authorName]=arr.push(objArr[i]);
}
You could do this:
var b = ... your object ...
var m = {};
b.map(function(v){(m[v.authorName]=m[v.authorName]||[]).push(v)});
Demonstration (open the console)
http://jsfiddle.net/azqRu/
var array = [
{bookName:"javascript",authorName:"john"},
{bookName:"java",authorName:"peter"},
{bookName:"j2ee",authorName:"john"},
{bookName:".net",authorName:"peter"},
{bookName:"oracle",authorName:"peter"}
];
var items = {};
for(var i = 0, ii = array.length; i < ii; i++) {
var cell = array[i];
items[cell.authorName] = items[cell.authorName] || [];
items[cell.authorName].push(cell);
}
alert(items["peter"]);

Categories

Resources