I have two arrays I want to merge them into one.
let signUps = [];
let usersInputs = [];
but each of them has been defined on different pages and therefore when I try
function mergeArray (){
var merged = signUps.concat(usersInputs);
document.getElementById('btn').innerHTML = merged
console.warn('added' , [merged] );
}
It says, on this occasion, usersInputs is not defined.
I have encountered this problem on so many occasion, maybe my lack of understanding. so how one approaches this problem?
On different pages you mean it's completely on a separate HTML document and you want to merge them together? In a web page whatever JavaScript you execute is available only inside of that webpage, not on a separate one.
You may either want to store your data on server, or pass it using form data, or best make use of localStorage or sessionStorage API in JavaScript to pass data from one page to another in a simple way.
Related
Disclaimer: I just started with JavaScripts/GoogleForms
I want to validate some data with a GoogleForm that I've created. Each section contains 5 multiple choice questions. In the example below I've shown how I add one question to the section.
// Sentence 1
sentence = form.addMultipleChoiceItem();
choices = [
sentence.createChoice('Answer I'),
sentence.createChoice('Answer II'),
sentence.createChoice('Answer III'),
]
sentence.setTitle(pred_1) // pred_1 -> Variable extracted from the dataset
.setChoices(choices);
In the end, I can build an excellent-looking form that contains 20 sections with five questions each based one a single dataset in my Google Drive Folder.
However, I want to extend this. I have ten different datasets that I want to have evaluated. A straightforward approach is copying the script ten times and pointing the script towards a different dataset. However, with this approach, I have to run the script ten times, and every time I adjust something (e.g. in the Title/HelpText), I have to rerun all the scripts.
I was thinking there should be a solution where I just read all the file inside my Drive folder and run my main scripts that produces the forms for multiple dataset. I was thinking something like this:
function createForms() {
// Folder with data
var folder = DriveApp.getFolderById('FOLDERID')
// List files
var files = folder.getFiles()
for (var i = 1; i < 11; i++) {
createMultipleChoiceForm(files[i]) // Function that creates a single form
}
}
This does unfortunately does not work at all (it does not give any errors, but does not produce anything).
EDIT:
This does actually work, but it creates a new form every time without saving it (it overruns the previous version).
I final approach is of course concatenating all the 10 datasets, and sample them from within the Google Script editor. But I still want to end up with ten different form (so I can send them to different people.
I hope anyone can give me some pointers in the right direction.
Thanks!
The Folder.getFiles() method returns a FileIterator object rather than an array. The code in the question ends up calling createMultipleChoiceForm() with an undefined parameter every time.
To make it work, replace the for loop with this pattern:
const files = folder.getFiles();
while (files.hasNext()) {
createMultipleChoiceForm(files.next());
}
Let's say I have a script that runs a for loop that outputs x number of new script lines depending on what a user answers. This script's main purpose is to construct an outgoing SOAP message with defined parameters. The for loop creates additional parameters to add to the existing parameters that are hardcoded into the script. How do I add these new parameters from the for loop to the script? Basically, add lines of script within the script (print to the script)... I can include the script if need be.
Thanks!
You can use arrays with array.push(element) to make this happen. Arrays are incredibly powerful for processing like what you're describing, and we use them in our app for this purpose.
var scripts = [];
for(var i=0; i < data; i++) {
scripts.push(data[i]);
}
Then if you need a single string at the end you can use join like this scripts.join(). Note that you could also add line returns if you want too.
However, it sounds like you do want to process the results of the scripts in order, at which time you can just loop through that array and send the parameters to another function one at a time.
There are a lot of functions in the array object that will help with what you're trying to do. Here is a link to the MDN docs for your reference.
I have defined a get_queryset() method where i want to return two queryset objects like:
get_queryset(self):
r1 = Books.objects.filter(auther_id=1);
r2 = Books.objects.filter(~Q(auther_id=1));
return r1,r2
The call to this method comes from a javascript file with an ajax call.
Now i want to access r1 and r2 individually in the js file.
I tried doing
r=chain(r1,r2) // in views.py
r.r1.fieldName and r.r2.fieldName // in js file.
I am unsure how could i unchain the itertools object. If it is not possible can anyone tell me about some other approach for this.
I also see method like ifilter() and etc to access the object but i want to access it in js.
EDIT: I have edited the code . CAN i club both query like
Books.objects.all()
and in the js do something to get two lists one having books by auther_id 1 and second list having other auther ids.
This doesn't make any sense at all. You can't 'unchain' something; the whole point of chain is that it makes the individual elements into one single undifferentiated item, there is no way of telling where one of the originals ended and the next started.
If you want to access two querysets individually in the template, then pass two individual querysets.
I have inherited some code that uses the Play! framework which has scala.html files that have javascript in them. Play! and scala are all new to me.
One of the javascript functions does a post and gets back a JSON object. It then loops through the JSON object and creates an array.
var myArray = [];
function createArray(){
$.post('/createArray', $('#arrayForm').serialize()).done(function( data ) {
var obj1 = JSON.parse(data);
$.each(obj1, function(idx, obj) {
myArray.push(obj.name);
});
});
return true;
}
It then uses this array (of strings) to create a text input field that does autocomplete using the data in the array.
I want/need to convert this text input to a select dropdown using the Play! #select but the options arg for #select wants a List object (or Map or Seq - just figured List would be easier since I already have an array of strings).
If I manually create the List object, it works fine.
#select(pForm("equipName"), options(scala.collection.immutable.List("Yes","No")))
The problem is I cannot figure out how to convert the myArray array to a List object which I can then pass to the #select options.
I have found a lot of posts that talk about converting a scala List to an array but can't find a way to go the other way. I am hoping it is an easy thing that I can't seem to figure out.
Thanks in advance for the help.
You can not do that. And more precisely - you do not want to do that.
So basically your play application run on server. In your Play application all those .scala html files are compiled to generate some functions.
Now, when a play application receives a request from a client browser, it gets mapped to some controller by by router. The controller does some processing and finally take one of these above functions ( lets say for index.scala.html we get views.html.index ) and call this function with some parameters.
These functions returns some text which is then sent to the client's browser as HTTP response with response header Content-Type:text/html; charset=utf-8 which tells the browser to treat this text as html.
Now, the browser renders the html which has embedded JavaScript and hence runs the JavaScript. So... basically your JavaScrpt code does not exist on server... for play all of it is just text.
Both of these Scala code and JavaScript code are executed at very different times, at different computers and in different environments hence you can not do whatever you are saying.
I have a component which writes/generates javascript from a server side renderer. This component can be used in multiple times in a same page. However, once the page is loaded I have to collect all the variables or JSO written by this multiple components in the page. How can I do this so that I will have a collection of all the variables or JSO?
For e.g. If this component (lets say ) is used twice in the page then it emits two javascript block on client/browser -
var arr1 = new Array['First', 'Second'] and
var arr2 = new Array['Third', 'Fourth'].
In order to make a final rendering I have to combine these two arrays.
You will need to be a little more specific about your problem, maybe with an example page but here are some thoughts.
If you have a server-side component that writes JavaScript during page generation, I would generate a function call each time, something like:
Component_appendArray(['First', 'Second']);
...
Component_appendArray(['Third', 'Fourth']);
then ensure that you have your function Component_appendArray() defined before these calls:
var globalArray = [];
function Component_appendArray(array)
{
globalArray = globalArray.concat(array);
}
At the end, globalArray should contain:
['First', 'Second', 'Third', 'Fourth']
Hope this helps.
Although I do not understand the entire scenario, let me suggest that if you are printing out variables throughout the HTML in order to use them after the page loads, that you instead use hidden input fields. I see this often, where variables are used to pass values to a function or a script at the bottom of the page, but using the values of hidden input fields allows you to get all your scripts out of the content areas. It makes for a cleaner solution.