I'm relatively new to AngularJS, so maybe my question is stupid, but I cannot find an answer.
I have prepared example to explain my problem, it can be found here.
When controller instantiates it has some values passed from parent scope. In my example:
$scope.modelId = 2;
$scope.sizeA = 2; // this is initial values
$scope.sizeB = 180; // for select elements
Then controller requests from server other values for the lists. It loads:
"sizesA": [1, 2, 6, 9, 10],
"sizesB": [120, 180, 300]
After data is loaded, we can see initialized selects on page:
SizeA: <select ng-model="sizeA" ng-options="s for s in model.sizesA"></select>
SizeB: <select ng-model="sizeB" ng-options="s for s in model.sizesB"></select>
sizeB is initialized to value 180 (as expected), but sizeA is initialized to value 6 (with index 2) instead of value 2.
When I specify $scope.sizeA = 9; in controller (there is no index 9, but there such value) it selects item with value 9.
So the question: how can I tell angular to use value
Add track by:
<select ng-model="sizeA"
ng-options="size for size in model.sizesA track by size"></select>
Related
LogicApps Azure:
I have this array, and i need a function for get the value max for each property.
[
{
"limMec": 18,
"limMed": 6,
"maxCons": 1,
"maxImp": 188.23,
"maxVeh": 7
},
{
"limMec": 12,
"limMed": 6,
"maxCons": 10,
"maxImp": 200.66,
"maxVeh": 1
},
{
"limMec": 4,
"limMed": 9,
"maxCons": 1,
"maxImp": 1,
"maxVeh": 2
}
]
I need a function, not variables !!!
I have not found multiples functions for have a subarray with the differentes results.
Someone know?
With this i can get the value of an element, but not max of the collection:
max(body('Seleccionar')[1]['limMec'])
For this requirement, I provide a sample below for your reference:
1. I initialize a variable named data and store the same data with yours to simulate your situation.
2. Then add a "Select" action and click "Switch Map to key value mode", choose the variable data into "From" box and write expression item()?['limMec'] into "Map" box.
3. Now, initialize a variable result and use the expression max(body('Select')).
4. After running the logic app, we can get the max value of limMec.
let x = [1, 2, 2, 3, 3, 3, 1].reversed()
for element in x.method_name() {
print(element)
}
This returns
Value of type 'ReversedCollection<[Int]>' has no member 'method_name'.
Why? How do I reference the method I have created and have it do the functions I need it to do?
However, if I use the below, the problem seems to disappear. I would just like to pass in an array and do let the function do all, i.e.:
let x = Array([1, 2, 2, 3, 3, 3, 1].reversed())
Just in case you don't fully understand the motivation behind this overload of reversed returning a ReversedCollection instead of an Array, a ReversedCollection is just a "reversed view" of your original array. It is not a reversed copy of the original array. This is to save time and space, like a "lazy" collection. See this post for more details.
This is why you need the Array(...) initialiser to turn the reversed collection back into an array. You are opting out of the laziness.
On the other hand, there is another overload of reversed that returns an Array directly. Normally this overload is not selected because it is defined in a less specific type - Sequence, as opposed to Array. You need to give enough information about the type to use this overload:
let x: [Int] = [1, 2, 2, 3, 3, 3, 1].reversed()
If I have the following array:
x = double([1, 1, 1, 10, 1, 1, 50, 1, 1, 1 ])
I want to do the following:
Group the array into groups of 5 which will each be evaluated separately.
Identify the MAX value each of the groups of the array
Remove that MAX value and put it into another array.
Finally, I want to print the updated array x without the MAX values, and the new array containing the MAX values.
How can I do this? I am new to IDL and have had no formal training in coding.
I understand that I can write the code to group and find the max values this way:
FOR i = 1, (n_elements(x)-4) do begin
print, "MAX of array", MAX( MAX(x[i-1:1+3])
ENDFOR
However, how do I implement all of what I specified above? I know I have to create an empty array that will append the values found by the for loop, but I don't know how to do that.
Thanks
I changed your x to have unique elements to make sure I wasn't fooling myself. It this, the number of elements of x must be divisible by group_size:
x = double([1, 2, 3, 10, 4, 5, 50, 6, 7, 8])
group_size = 5
maxes = max(reform(x, group_size, n_elements(x) / group_size), ind, dimension=1)
all = bytarr(n_elements(x))
all[ind] = 1
x_without_maxes = x[where(all eq 0)]
print, maxes
print, x_without_maxes
Lists are good for this, because they allow you to pop out values at specific indices, rather than rewriting the whole array again. You might try something like the following. I've used a while loop here, rather than a for loop, because it makes it a little easier in this case.
x = List(1, 1, 1, 10, 1, 1, 50, 1, 1, 1)
maxValues = List()
pos = 4
while (pos le x.length) do begin
maxValues.add, max(x[pos-4:pos].toArray(), iMax)
x.Remove, iMax+pos-4
pos += 5-1
endwhile
print, "Max Values : ", maxValues.toArray()
print, "Remaining Values : ", x.toArray()
This allows you to do what you want I think. At the end, you have a List object (which can easily be converted to an array) with the max values for each group of 5, and another containing the remaining values.
Also, please tag this as idl-programming-language rather than idl. They are two different tags.
If I have an array of variables in Ruby:
a = 4
b = 7
c = 1
array = [a, b, c]
How can I get access to the name of the variable that has the highest value? (In this example b) I want to retrieve a reference to the element with the highest value, to be able to later manipulate it:
b += 10
I tried array.max but that just returns the max value 7
When you build an array by writing array = [a, b, c], the spots in the array do not retain any kind of association with the variables named a, b, and c, so you there is no way to do exactly what you are talking about. Those variables can change without affecting the array.
You could change the way you are storing your data in order to make this possible, but without knowing what data you are storing it is hard to recommend a good way to do it. You could consider storing your data inside a hash instead of in loose variables and an array:
hash = { a: 4, b: 7, c: 1}
Ruby does not support passing objects by reference, which is essentially what you are trying to do. When you call array.max, you are passed a copy of the Fixnum 7, and assignment and modification will be applied to this copy.
That is, you can not store a reference to this Array element, and modify it later. You can modify the maximum value on the spot, however, using:
array[array.index(array.max)] = array.max + 10
#=> 17
array
#=> [4, 17, 1]
Note that when there are duplicate values in the Array, array.index(array.max) will return the index of the first one.
Storing the index for later use is not a solid solution, since, while Arrays in Ruby are ordered, the Array or its elements can be modified between the point you retrieve the index, and the point where you decide to change the corresponding value.
Another answer suggests there's a hack to mimic pass-by-reference behaviour, but in doing this, you're working against the intention of the language, and it could lead to a lot of pain down the line.
If you are trying to manipulate the maximum of three values, then, you are better off doing something like this:
array.max + 10 # or any other manipulation for that matter.
If you are asking what you are asking for academic purposes, then, you could use Ruby's Binding to inspect values of local variables.
a = 4
b = 7
c = 1
array = [a, b, c]
# Select variable whose value matches with maximum of three elements
# Variable's name as symbol is obtained by this process
var = binding.send(:local_variables).select do |i|
array.max.eql? binding.local_variable_get(i)
end.first
#=> :b
# Update value of variable using symbol representing its name
binding.local_variable_set(var, binding.local_variable_get(var) + 10)
puts b
#=> 17
You could also use Binding#eval to manipulate the variable, for e.g.:
binding.eval("#{var.to_s} += 10")
#=> 17
Note that we need to use var.to_s as var contains a symbol, and we need to convert it to string to get variable name string that can be used in eval.
You can use Array#index for this..
1.9.3-p484 :008 > max_elem = array.index(array.max)
=> 1
1.9.3-p484 :009 > array[max_elem] = array[max_elem] + 10
=> 11
1.9.3-p484 :010 > array
=> [4, 17, 1]
1.9.3-p484 :011 >
Ext.Array.slice() is used to grab a range of elements from given begin and end figure. I'm slicing out 10 elements from a large array.
Now, I'd like to slice 10 elements from the array which meet certain condition (like passing a function to slice() which returns truthy value), instead of grabbing 10 values directly. How can I do that?
Which version of ExtJs are you using? If you are on version 4 or above checkout the Array.filter method.
E.g.
function isBigEnough(element, index, array) {
return (element >= 10);
}
passed = [12, 5, 8, 1, 4,15].filter(isBigEnough);
//returns [12,15]