Array List in XPages how to parse - arrays

I have some values in array like this below (arr1);
I need to make a kind of unique arrayList which has to collect and make a new list with values before "~"
If the value before the "~" is in the new list, the value after "~" has to append into existing value of array.
var arr1:Array = new Array();
arr1[0] = "test 1 ~ 781.102";
arr1[1] = "test 2 ~ 981.112";
arr1[2] = "test 2 ~ 191.222";
arr1[3] = "~ 431.332";
arr1[4] = "test 1 ~ 121.332";
arr1[5] = "test 3 ~ 121.442";
arr1[6] = "test 3 ~ 201.552";
New List should be this...
var nArr:Array = new Array();
nArr[0]= "test 1 - (781.102,121.332)";
nArr[1]= "test 2 - (981.112,191.222)";
nArr[2]= "test 3 - (121.442,201.552)";
nArr[3]= "undefined - (431.332)";

Use .split("~") to separate substrings divided by ~. The result is an array.
Use a JavaScript object with keys and values. The key is "test 1", "test 2", ... and the value is a JavaScript array. Add for every element of arr1 the number to array for the given key.
Finally, create an array from your object and convert the array values into "( ..., ...)"
Your code would look like this:
var obj = {};
for (var i = 0; i < arr1.length; i++) {
var keyAndValue = arr1[i].split("~");
var key = keyAndValue[0].trim();
var value = keyAndValue[1].trim();
var array = [];
if (key in obj) {
array = obj[key];
}
array.push(value);
obj[key] = array;
}
var nArr = [];
for (key in obj) {
nArr.push(key + " - (" + obj[key].toString() + ")");
}

What you're using are not ArrayLists, they're Arrays.
To avoid reinventing a wheel, search the web for java.util.ArrayList if you want best practice on how to manipulate an ArrayList.
The best advice is to use Java constructs rather than SSJS Arrays. They are documented more widely on the web (because they're used beyond just XPages) and will get you used to handling these kinds of things in Java business logic in the future. Look at Lists and Sets and Maps.
What you're actually wanting is not a unique list, it's a Map of unique values (to the left of a tilde) followed by a concatenated string of entries that use that key.

Related

How prevent Object.keys() sort?

The problem with the ECMA standard for sort of Object.keys() is known:
Object.keys() handle all keys with integer (example: 168), including integer as strings (example: "168"), as a integer. The result is, both are the same (168 === "168"), and overwrite itself.
var object = {};
object["168"] = 'x';
object[168] = 'y';
Object.keys(object); // Array [ "168" ]
object[Object.keys(object)]; // "y"
Interestingly, all keys (including pure integer keys) are returned as a string.
The ecma262 wrote about this: All keys will be handle as a integer, expect the key is a String but is not an array index.
https://tc39.es/ecma262/#sec-ordinaryownpropertykeys
That should tell us: 168 === "168". A toString() do not solve the problem.
var object = {};
object[[3].toString()] = 'z';
object[[1].toString()] = 'x';
object[[2].toString()] = 'y';
Object.keys(object);
// Array(3) [ "1", "2", "3" ]
Paradoxically, in this case, only integer apply as "enumerable" (it's ignoring array.sort(), that sort also strings with letters.).
My question about this is simple: How can i prevent the sort function in Object.keys()? I have testet the Object.defineProperties(object, 1, {value: "a", enumerable: true/false}), but that mean not realy enumerable in the case of integer or string or integer-like string. It means only should it be counted with or not. It means "counted" like omit (if it false), not "enumerabled" like ascending or descending.
A answere like that is not a good answer: Please use only letters [a-zA-Z] or leastwise a letter at the first position of keyword.
What I want: That the keys are not sorted, but output in the order in which they were entered, whether integer, string or symbol.
Disclaimer: Please solutions only in JavaScript.
Javascript Objects are unordered by their nature. If you need an ordered object-like variable I would suggest using a map.
To achieve what you're looking for with a map instead of object you'd do something like the below:
var map1 = new Map();
map1.set("123", "c");
map1.set(123, "b");
var iterator1 = map1.keys();
var myarray = [];
for (var i = 0; i < map1.size; i++) {
myarray.push(iterator1.next().value);
}
console.log(myarray);
// Array ["123", 123]
Unfortunately it's not compatible with IE and I'm not sure how else you could achieve what you need without it. A quick Google did return something about jQuery maps, though.
If you don't want to use jQuery and still need to support IE some points are below:
Is there anything stopping you using an array rather than JS object to store the data you need? This will retain the order per your requirements unlike objects. You could have an object entry in each iteration which represents the key then use a traditional foreach to obtain them as an array. I.e.
The array:
var test_array = [
{key: 123, value: 'a value here'},
{key: "123", value: 'another value here'}
];
// console.log(test_array);
Getting the keys:
var test_array_keys = [];
test_array.forEach(function(obj) { test_array_keys.push(obj['key']); } );
// console.log(test_array_keys);
Then if you needed to check whether the key exists before adding a new entry (to prevent duplicates) you could do:
function key_exists(key, array)
{
return array.indexOf(key) !== -1;
}
if(key_exists('12345', test_array_keys))
{
// won't get here, this is just for example
console.log('Key 12345 exists in array');
}
else if(key_exists('123', test_array_keys))
{
console.log('Key 123 exists in array');
}
Would that work? If not then the only other suggestion would be keeping a separate array alongside the object which tracks the keys and is updated when an entry is added or removed to/from the object.
Object Keys sorted and store in array
First Creating student Object. then sort by key in object,last keys to store in array
const student={tamil:100, english:55, sci:85,soc:57}
const sortobj =Object.fromEntries(Object.entries(student).sort())
console.log(Object.keys(sortobj))
use map instead of an object.
let map = new Map()
map.set("a", 5)
map.set("d", 6)
map.set("b", 12)
to sort the keys (for example, to update a chart data)
let newMap = new Map([...map.entries()].sort())
let keys = Array.from(newMap.keys()) // ['a','b','d']
let values = Array.from(newMap.values()) // [5,12,6]

Change array string value from another array with index

I am trying to change my string value in an array after shuffling another array, how am i to do this?
Example:
var array1 = [1,2,3,4,5,6]
var stringarray = ["\\(array1[0]) = 1")
array1.shuffle()
print(stringarray)
How am i to change the original stringarray value to the new shuffled value?
Thank you
The task:
#IBAction func nextQuestion(_ sender: Any) {
if levelSelected == 1 {
questionLabel.text = standardRules.randomElement()
}
players.shuffle()
print(players)
standardRules has a string value that takes the value of players[0]
Essentially what i am trying to do is this:
I am trying to grab 2 random values that are not the same in a string like this
var players = ["Jack, John, Michael, Peter"]
var playersArray = ["\(players.randomElement) and \(players.randomElement) has to battle")
How am i to do this, so it grabs 2 different values?
You could make it very easy by this code :
var players = ["Jack", "John", "Michael", "Peter"]
// get the first random element
var random1 = players.randomElement()
//delete that element so you can't duplicate it
players = players.filter{$0 != random1}
//get your second radom element
var random2 = players.randomElement()
//add your element again
players.append(random1!)
Looks like you're missing some core concepts of the Swift language. When you create your stringarray it makes a call of array1.description and stores the result into an array. From that point any modifications of the original array will not change anything in stringarray.
So if you want to pick two different players from an array you need to do something like that:
let index1 = Int.random(in: (0 ..< players.count))
var index2: Int
repeat {
index2 = Int.random(in: (0 ..< players.count))
} while index1 == index2
let matchText = "\(players[index1]) and \(players[index2] has to battle)"
I would try replacing:
var stringarray = ["\\(array1[0]) = 1")
array1.shuffle()
with:
var stringarray = array1.shuffle()

swift - using .map on struct array

i have a struct array that i want "break up" into smaller arrays that can be called as needed or at least figure out how i can map the items needed off one text value.
the struct:
struct CollectionStruct {
var name : String
var description : String
var title : String
var image : PFFile
var id: String
}
and the array made from the struct
var collectionArray = [CollectionStruct]()
var i = 0
for item in collectionArray {
print(collectionArray[i].name)
i += 1
}
printing partArray[i].name gives the following result:
pk00_pt01
pk00_pt02
pk00_pt03
pk01_pt01
pk01_pt02
pk01_pt03
pk01_pt04
pk01_pt05
pk01_pt06
pk01_pt07
pk01_pt08
this is just some test values but there could be thousands of entries here so i wanted to filter the entire array just by the first 4 characters of [i].name i can achieve this by looping through as above but is this achievable using something like .map?
I wanted to filter the entire array just by the first 4 characters of
[i].name
You can achieve this by filtering the array based on the substring value of the name, as follows:
let filteredArray = collectionArray.filter {
$0.name.substring(to: $0.name.index($0.name.startIndex, offsetBy: 4)).lowercased() == "pk00"
// or instead of "pk00", add the first 4 characters you want to compare
}
filteredArray will be filled based on what is the compared string.
Hope this helped.
If you want to group all data automatically by their name prefix. You could use a reducer to generate a dictionary of grouped items. Something like this:
let groupedData = array.reduce([String: [String]]()) { (dictionary, myStruct) in
let grouper = myStruct.name.substring(to: myStruct.name.index(myStruct.name.startIndex, offsetBy: 4))
var newDictionart = dictionary
if let collectionStructs = newDictionart[grouper] {
newDictionart[grouper] = collectionStructs + [myStruct.name]
} else {
newDictionart[grouper] = [myStruct.name]
}
return newDictionart
}
This will produce a dictionary like this:
[
"pk00": ["pk00_pt01", "pk00_pt02", "pk00_pt03"],
"pk01": ["pk01_pt01", "pk01_pt02", "pk01_pt03", "pk01_pt04", "pk01_pt05", "pk01_pt06", "pk01_pt07"],
"pk02": ["pk02_pt08"]
]
Not sure if i am understanding you correctly but it sounds like you are looking for this...
To create a new array named partArray from an already existing array named collectionArray (that is of type CollectionStruct) you would do...
var partArray = collectionArray.map{$0.name}

Linking two arrays in Actionscript 3

Very new to actionscript,
Im trying to link two arrays. Basically I have a word array of 8 words and I have a movie clip array of 8 movieclips. My aim is to link the two arrays so that the user must click the right movie clip that matches the word that was displayed on screen.
All help is greatly appreciated!!
var listAry:Array = [];
var orangeJuice:Object = new Object();
orangeJuice.name= "Orange Juice";
orangeJuice.matchingImage=oj;
listAry[0]=orangeJuice;
////etc etc
There you go buddy. Hope that helps if you have any questions just ask.
Another way to do this is with a Dictionary.
var foodionary:Dictionary = new Dictionary();
foodionary["Orange Juice"] = oj;
foodionary["Sandwich"] = sand;
//etc...
for(var key:String in foodionary) {
trace(key + " matches with " + foodionary[key].id); //assuming your images have ids
}
For random access, though, you'll still need an array (or a Vector):
function displayword(){
randomnumber = Math.floor(Math.random() * randomlistword.length);
trace("random number = " + randomnumber);
var chosenword = randomlistword[randomnumber];
randomword.text = chosenword
randomword.img = foodionary[chosenword];
randomlistword.splice(randomnumber, 1);
trace("randomlistword array: " + randomlistword);
}//close displayword function

Comparing two arrays in swift, with different lengths

I want to compare two arrays with each other and append the value that is not in the other array to a new array. The problem now is that all of the values that does not equal the other array already get appended, but I want only the values that are new in the other array getting appended.
I hope that the problem is clear. Sorry if it's a very vague question. I try to be clear haha.
The code and output is printed below:
// Iterate through all possible values
for i in 0...messages.count-1{
var match = false
for r in 0...self.messages.count-1{
println("NIEUWE" + messages[i].getID() + "OUDE" + self.messages[r].getID())
if(messages[i].getID().toInt() == self.messages[r].getID().toInt()){
var match = true
println(match)
break
}
}
if (!match) {
newArray.append(messages[i])
println(newArray)
}
}
Output:
NIEUWE170OUDE170
NIEUWE170OUDE171
true
[PostDuif.Message]
NIEUWE171OUDE170
true
[PostDuif.Message, PostDuif.Message]
NIEUWE172OUDE170
true
This "I want to compare two arrays with each other and append the value that is not in the other array to a new array" is just 'set difference'
var s1 = Set(["a", "b", "c"]) // this to be similar to your need
var s2 = Set(["b", "c", "d"])
var s3 = s2.subtract (s1)
As such:
9> var s3 = s2.subtract(s1)
s3: Set<String> = {
[0] = "d"
}
Note that you have subtract, intersect, and union with inPlace options as methods on the Set type. New to Swift 1.2.

Resources