SSJS remove item from an array - arrays

In ssjs I have an array that contains jsonobjects. this array is stored in a scope variable and displayed in a repeat control:
<xp:repeat id="rptAssessors" value="#{sessionScope.tmpAssessors}" var="obj" indexVar="idx">
From the repeat control I would like to add the option to remove individual items from the array e.g. via onclick event on a button for each row:
<xp:eventHandler event="onclick" submit="true" refreshMode="partial" immediate="true" refreshId="dlgContent"> <xp:this.action> <xp:executeScript
script="#{javascript:removeAssessor(idx);}"> </xp:executeScript> </xp:this.action></xp:eventHandler>
My removeAssessor function looks as followed:
function removeAssessor(idx){
var jsonObjArr = sessionScope.get("tmpAssessors");
print("before elements " + jsonObjArr.length)
print(idx + 1)
jsonObjArr.splice(idx,1);
print("after elements " + jsonObjArr.length)
sessionScope.put("tmpAssessors",jsonObjArr);
}
I notice the splice() method does not work. I read on an xsnippet that this method does not work in SSJS and an alternative is posted as an xsnippet but this is for arrays containing strings
https://openntf.org/XSnippets.nsf/snippet.xsp?id=remove-an-entry-from-an-array-of-strings
Anyone can tell me how to remove an item from the array that is not a string?

Array.splice does work in SSJS, but not in the "inline way" you might be used to from CSJS: In CSJS the modification is done inline (no new array is created) and the removed elements are returned. In SSJS, the original array is not modified and the return value is a copy of the array excluding the spliced elements.
If you replace jsonObjArr.splice(idx,1); with jsonObjArr=jsonObjArr.splice(idx,1); your code should work fine.

Related

How to store data to an array inside foreach loop in Laravel?

I have a collection of $linesheetItems, now I need to loop these $linesheetItems inside a foreach loop and store a seasons array by using line sheet item's season code ($linesheetItem['season']). But according to my current code, it returns an empty array.
Code:
$seasons = [];
foreach($linesheetItems as $linesheetItem) {
$seasons = Season::where('code', $linesheetItem['season'])->get();
}
dd($seasons);
How to achieve this, and what are the modifications should I do to my code?
In your code, you are overriding the $seasons variable each time the loop runs. In order to add an item to an array you have to set $seasons[] = Season::where('code', $linesheetItem['season'])->get();. This will always push a new item into the array. If you want to have custom keys on the array, you can do $seasons['your-key'] = Season::where('code', $linesheetItem['season'])->get();

How to use Array in JEXL?

Using JEXL, I am trying to initialize array and than adding elements into it, however below code gives me 'unsolvable property '0' error.
var abc=[];
abc[0]=5;
1) How can I initialize empty array and keep adding values in it?
2) Can I use it like List, where I do not need to specify size at the time of initialization ?
in JEXL syntax you can initialize objects with new function.
Other option is to add to context arraylist:
This is a working example with jexl2:
JexlEngine jexl = new JexlEngine();
String jexlExp = "var abc=new(\"java.util.ArrayList\", 1);abc[0]=5";
Expression e = jexl.createExpression( jexlExp );
List<Integer> abc = new ArrayList<>(1);
JexlContext jc = new MapContext();
//jc.set("abc", abc ); second option to add arraylist to context
Object o = e.evaluate(jc);
In JEXL, the syntax [] creates a Java array, not a List. As an array, it has a fixed size, so you cannot add values to it. However, JEXL 3.2 has a new syntax for creating an ArrayList literal. Basically, you add ... as the final element.
So in JEXL 3.2, your example could be written as:
var abc=[...];
abc.add(5);
See the JEXL literal syntax reference for more information.

appendRow from an array

If I have an array, [Joe, John, Adam, Sam, Bill, Bob] and I want to try to add this to a new row by doing SpreadsheetApp.getActive().getSheetByName('Sheet4').appendRow([array]); , what happens is that the entire list of names goes into 1 cell. Is there a way to break this up so they file away into the same row, but different columns? I need to continue using appendRow however.
I get this:
But I really want to have it look like this:
var my2DArrayFromRng = datasheet.getRange("A:A").getValues();
var a = my2DArrayFromRng.join().split(',').filter(Boolean);
var array = [];
for (d in a) {
array.push(a[d]);
}
SpreadsheetApp.getActive().getSheetByName('Sheet4').appendRow([array.toString()]);
You are converting your array to a string before you post it which is causing your issue.
Do not use the array.toString() method inside append row. Instead just append the array as it is.
SpreadsheetApp.getActive().getSheetByName('Sheet4').appendRow(array);

Actionscript 3: each line from text file as a element in array

So my problem is that trace within the function does trace the first element of the array, but the trace outside if the function does not. I do declare the array variable outside the function, but the data wont save to the array variabel.
var oppgaveLoader:URLLoader = new URLLoader();
oppgaveLoader.load(new URLRequest("oppgaver.txt"));
var oppgaveNr = 0
//store line of text on an array called oppgaver
var oppgaver:Array = []
var oppg:Array = new Array()
oppgaveLoader.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(e:Event){
oppgaver = e.target.data.split(/\n/)
trace(oppgaver[0]) //This one traces the frist item in the array
}
trace(oppgaver[0])//This one does not trace the first one in the array
Does anyone know why and/or how to fix it if posible?
The file "oppgaver.txt" is located in the same directory as my .fla file
The file "oppgaver.txt" is laid out like this (the text is in norwegian, but each line is going to be a item in the array):
Hvor gjelder forbudsskilt hvis ikke annet er oppgitt?
Hvordan foretar du best mulig bremsing og unnastyring?
Hvordan bør du normalt plassere bilen på en vanlig 2-felst vei?
It's a synchronicity problem.
the last trace happens immediately after you set up your arrays, but those arrays are still empty.
only when the onLoaded function gets called, asynchronously by the URLLoader, they get populated and you can trace their values.
that event listener basically lets you react to an event that happens in the future at some point.

Accessing element of an perl Array element

I have a perl code which read csv file. It contains grid data which needs to be updated at the front end.
First, here is the perl code which reads data and formats it so that the data can be pushed to front end for display.
my #array;
for my $column ($csv->column_headers) {
my $json = encode_json([ map { $_->{$column} } #$data ]);
push(#array, "$json;");
}
The final data is the #array which is passed to front end javascript code. The contents of #array is as follows.
["1","2"]; ["dd","ddd"]; ["wow","cool"]; ["HOLD","HOLD"];
This data is actually 4 columns with column header names as Id, Name, Comment and type. All these data are bundled up together in #array and passed to Javascript.
var header=[];
header[0] = #array[0];
}
This code above displays the below output if I do a console.log(header[0]); It means it is displaying the first element of the array. but I want to display the first element's element.
["1", "2"]
whereas it should display below output.
["1"]
In short, I want to know how can I access array elements elements. I tried using below code but it didn't work. Can someone please suggest?
var header=[];
header[0] = #array[0][0];
I am ultimately trying to put this data in grid by using below code.
for (var i=0;i<row_cnt;i++){
var row={};
row["Id"]=Id[i];
row["Name"]=Name[i];
row["Comment"]=Comment[i];
row["type"]= type[i];
data[i]=row;
}
where Id[i] will corresponding to "1" in first loop and "2" in second loop. Similarly it will generate data for other columns. These are then assigned to rows and updated in grid.
As per matts suggestion, I edited the code like this
my $json = encode_json($data);
for my $column ($csv->column_names) {
push(#data_array, "var $column= $json;");
}
Now it displays below values at every cell of the grid.
[object Object]
Building on the answer to your previous question, I think you just need to swap out the loop at the end for this:
my $json = encode_json($data);
print "var data = $json;\n";

Resources