I have a variable of type array of object for which I want to find the index of an object according to 2 fields of the object which represents the unique key. With ARRAY_OF_INDEX I can do this operation with a unique key in a unique field, but not with a key composed of 2 fields. Any idea?
Here is a Workaround for one key:
I guess its becoming tricky with two key fields since even the easyer operations are often not provided for objects / object-arrays.
@youri.regnaud I’m thinking about how to approach this and what I would like to do is concatenate the keys and store the result into an array. I don’t think this is possible with the expression editor. Open to creative solutions, here, but if it were me I would make a custom widget that parses it and spits out the information I want. Sometimes it’s just easier to make a little widget that does what I want!
customer widget will be a good way for your requirements, Here is the JS code you can refer to:
sampleArray is your input:
var sampleArray = [
{“id”: 1, “name”: “tony”,“age”:11},
{“id”: 2, “name”: “tony1”,“age”:22},
{“id”: 3, “name”: “tony2”,“age”:33},
{“id”: 4, “name”: “tony3”,“age”:44}
];
//getting the object by its id and name
var output = sampleArray.find(object => object.id === 3 && object.name === "tony2" );
Maybe one row code in Customer Widget
Hmm, super interesting. For the sake of the thought experiment, trying to do this without a widget…
col1 | col2
=============
a | x
b | y
c | z
1. [expression] map_to_text_list(col1), [a,b,c]
2. [expression] map_to_text_list(col2), [x,y,z]
3. [arrays] get length of either (assuming equal), col1_size=3
4. [arrays] join to string, col1_string = a,b,c
5.0[expression] set i to 0
5.1[arrays] get from index in array, col2[i] = col2_var = x
6. [expression] substitute(col1_string, ',', col2_var)
7. [etc] Repeat steps 5 and 6, i++, for col1_size times --> now you have col1_string=(axbycz)
8. [expression] find("by", col1_string) = target_index = 2
9. [arrays] get from index in array(my_full_array, target_index)
This method relies on making the values into pairs, i.e. 'col1[0]' + 'col2[0]' = [ax]
and then searching for the pair you want. However, I’m not sure there is a way to do this without manually making n functions for a list of length n.
Widgets seem to probably be your best path here, unless I’m missing something.
(or, I guess, just display the list as an Interactive Table and filter the columns, but then you need to force a user to click the table)
edit: as is tradition, found a similar method on SO, linked here:
let a = [1,2,3,4,5]
let b = [6,7,8,9,10]
let combine = a.map((e, i) => [e, b[i]]);
console.log(combine);
now combine
is ([1,6], [2,7], ...)
Thanks for your feedback. This is a nice proof that additional expressions for handling array of objects would be useful. Custom expression editor can be also an idea after custom widget