I want to change the value of these elements with a "trim ()".
"this.currentPaaas.Requisicion.RefCFinancingFirst [0] = this.currentPaaas.Requisicion [0] .RefCFinancingForm.trim ();"
"this.currentPaaas.Requisicion [0] .RefCObjetoGasto = this.currentPaaas.Requisicion [0] .RefCObjetoGasto.trim ();"
But they are inside an array, the way I do it only changes a single value within the array.
How can I change all the values within that array?
currentPaaas contains the array Requisicion []
Image Example
From what has been stated in the question,i think what you are trying to do is to trim RefCFinancingForm of each element in currentPaaas?
If yes,This is what you need:
this.currentPaaas.Requisicion.forEach(x=>x.RefCFinancingForm =x.RefCFinancingForm.trim())
Related
I am creating array of Promises of type [MSGraphScheduleInformation?]. I want to limit the array count to 4, but the repeating param in the API is throwing errors. Below is my code:
For infinite array count the code looks like this:
var array = [Promise<[MSGraphScheduleInformation?]>] // This works
For array with limited count:
var array = [Promise<[MSGraphScheduleInformation?]>](repeating: Promise<[MSGraphScheduleInformation?]>, count: 4) // Throws error
Error I get with above line is - Cannot convert value of type 'Promise<[MSGraphScheduleInformation?]>.Type' to expected argument type 'Promise<[MSGraphScheduleInformation?]>'
Here is what I want eventually:
[Promise<[MSGraphScheduleInformation?] Object 1,
Promise<[MSGraphScheduleInformation?] Object 2,
Promise<[MSGraphScheduleInformation?] Object 3,
Promise<[MSGraphScheduleInformation?] Object 4]
How do I create an array with count and custom type?
As the error message says, you are passing a Type when an instance of that Type is expected.
The repeating initialiser of Array places the value specified by the repeating parameter into the first count indices of the array.
It does not, as your question implies you think, create an array whose elements are the type specified by the repeating array and is limited in size to count.
You should also note that repeating does not specify an initialiser to be called for each element. It is literally the item that will be placed in each index.
You could say something like
var array = [Promise<[MSGraphScheduleInformation?]>](repeating: Promise<[MSGraphScheduleInformation?]()>, count: 4)
You are now passing an instance of a Promise, but you will end up, with that same instance in the first 4 indices. :
[Promise<[MSGraphScheduleInformation?] Object 1,
Promise<[MSGraphScheduleInformation?] Object 1,
Promise<[MSGraphScheduleInformation?] Object 1,
Promise<[MSGraphScheduleInformation?] Object 1]
(I am ignoring whether Promise<[MSGraphScheduleInformation?]()> is even a valid initialiser).
Finally, count only specifies the initial size of the array. It isn't an upper or lower limit that somehow applies to the life of the array. You can remove and append items just like any array.
If you want to limit the number of items in the array you need to do that in your code.
If you want to pre-populate 4 promises, you can use a simple for loop, but that doesn't make a lot of sense. I don't see how you can create a promise before you know what it is promising; I.E. how the promise will be fulfilled.
This question already has an answer here:
Ruby Array Initialization [duplicate]
(1 answer)
Closed 3 years ago.
Recently I was playing with arrays and found out a weird behavior. I created a new array using Array.new.
arr = Array.new(4,"happy")
["happy", "happy", "happy", "happy"]
I appended a word into the second element in that array like shown below
arr[1] <<" Dino"
When I looked at arr, I was expecting an array with the second element having an appended word. But to my surprise array returned with all elements with the appended word.
["happy Dino", "happy Dino", "happy Dino", "happy Dino"]
How can this happen? Are we creating copies of the same string when we are creating the array? It doesn't happen if we use arr[1]= " Dino". Somebody can explain why this is happening?
Yes, you a right. See the ruby docs
When a size and an optional default are sent, an array is created with size copies of default. Take notice that all elements will reference the same object default.
You can initialize array in a way like this:
arr = Array.new(4){ 'happy' }
When we take Array.new(4, "Happy") it will create 4 elements with same object id. We can observe in irb arr[1].object_id => 2880099 , arr[2].object_id => 2880099 and remaining 2 elements also will return same object id.
So when we try like arr[1] << "Something" , this string will append to all the same object id's.
Now if we take another element for arr like arr.push("newString"). Now if see the last element object id arr.last.object_id => 4889988,it's different now.
So if we try same command arr[1] << "Something" it won't be effect to newly inserted element because object id is different.
if you don't want this behaviour the use Array.new(4){ 'String' }
How to create an array in VTL and add contents to the array? Also how to retrieve the contents of the array by index?
According to Apache Velocity User Guide, right hand side of assignments can be of type
Variable reference
List item
String literal
Property reference
Method reference
Number literal
ArrayList
Map
You can create an empty list, which would satisfy all your needs for an array, in an Apache Velocity template with an expression like:
#set($foo = [])
or initialize values:
#set($foo = [42, "a string", 21, $myVar])
then, add elements using the Java add method:
$foo.add(53);
$foo.add("another string");
but beware, as the Java .add() method for the list type returns a boolean value, when you add an element to the list, Velocity will print, for instance, "true" or "false" based on the result of the "add" function.
A simple work around is assigning the result of the add function to a variable:
#set($bar = $foo.add(42))
You can access the elements of the list using index numbers:
<span>$foo[1]</span>
Expression above would show a span with the text "a string". However the safest way to access elements of a list is using foreach loops.
Creating an array is easy:
#set($array = [])
Putting an element into an array is also easy:
$array.add(23)
Getting an element from an array depends from your Velocity version.
In Velocity 1.6 you must use
$array.get($index)
Since Velocity 1.7 you can use the classic form:
$array[$index]
I haven't created an array in VTL but passed arrays to VTL context and used them. In VTL, you can not retrieve array contents by index, you only use foreach, as example this code is copied from my Dynamic SQL generation VTL Script:
#foreach( $col in $Columns ) SUM($col.DBColumn) AS ''$col.Name''#if($velocityCount!=$Columns.Count), #end #end
For this reason, we also can not have 2D arrays. When I needed an array to store 2 objects in a row, I used the workaround of defining a new class, and putting objects of that class in the single dimensional array.
What's the difference between arrays and hashes in Ruby?
From Ruby-Doc:
Arrays are ordered, integer-indexed collections of any object. Array indexing starts at 0, as in C or Java. A negative index is assumed to be relative to the end of the array—that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on. Look here for more.
A Hash is a collection of key-value pairs. It is similar to an Array, except that indexing is done via arbitrary keys of any object type, not an integer index. Hashes enumerate their values in the order that the corresponding keys were inserted.
Hashes have a default value that is returned when accessing keys that do not exist in the hash. By default, that value is nil. Look here for more.
Arrays:
Arrays are used to store collections of data. Each object in an array has an unique key assigned to it. We can access any object in the array using this unique key. The positions in an array starts from " 0 ". The first element is located at " 0 ", the second at 1st position etc.
Example:
Try the following in - irb.
bikes = Array.new
bikes = %w[Bajaj-Pulsar, Honda-Unicorn, TVS-Apache, Yamaha, Suzuki]
You have added 4 elements in the array.
puts bikes[3]
Yamaha,
Add a new element to position 5.
bikes[5] = "Hardly Davidson"
Hashes:
Like arrays, Hashes are also used to store data. Hashes points an object to another object. Consider of assigning a certain "meaning" to a string. Each time you refer that string, it refers its "meaning".
Example:
bikes = Hash.new
bikes = {
'Bajaj' => 'Pulsar 220, Pulsar 200, Pulsar 180 and Pulsar 150',
'Honda' => 'Unicorn, Shine and Splendor',
'TVS' => 'Apache, Star City, and Victor'
}
Try this now:
bikes['Bajaj']
You get => "Pulsar 220, Pulsar 200, Pulsar 180 and Pulsar 150"
An array is an ordered list of things: a, b, c, d
A hash is a collection of key/value pairs: john has a peugeot, bob has a renault, adam has a ford.
The two terms get "hashed" together these days. I think this is how it goes:
A "hash" will have key -> value pairs:
(top -> tshirt, bottom -> shorts, feet -> shoes)
And an "array" will typically have an index:
([0]tshirt, [1]shorts, [2]shoes)
But, right or wrong, you'll see things with key -> value pairs called "arrays", too.
I think the difference depends mainly on when and how you want to use them. You won't get into much trouble calling an array a hash, or vice versa, but you should know the difference.
Is there a simple way, instead of looping my entire array to fetch the first value of every inner array.
so in essence I have the following;
array = [['test', 'test2'...], ['test' ....]]
so I want to grab array[#][0] and store the unqiue values.
EDIT
Is there a similar way to use the transpose method for arrays with Hash?
I essentially want to do the same thing
Hash = {1=> {1=> 'test', .....}, 2=> {1=> 'test',....}
so at the end I want to have something like new hash variable and leave my existing hash within hash alone.... = {1 => 'test', 2=> 'test2'}
Not sure if I fully understand the question, but if you have a 2 dimensional array (array in array), and you want to turn that into an array of the first element of the second dimension, you can use the map function
firsts = array.map {|array2| array2.first}
The way map works is that it turns one collection into a second collection by applying a function you provide (the block) to each element.
Maybe this?
array.transpose[0]