How can I use expressions to reduce array of objects - arrays

I am in the process of creating a flow which, upon the trigger activating, sends a prompt to a user.
The user selects a response, and then the flow sends a message in the appropriate channel. The prompt items come from the list channels action. I can use body('ListChannels')?['value] to parse the response. The response is an array with each object looking like:
{
"id":"",
"displayName":"General",
"description":"Used to test in-development features",
"email":"",
"webUrl":""
}
I want to reduce each object to just the displayName key and use the resulting as the inputs for the next action, this is the accepted input for that action:
["General", "Channel2", "Channel3"]
Is there a way to convert this using the provided expressions/actions for power automate? I also need to use the response to lookup the corresponding array entry "id" for sending a message to that channel.

One possibility is to use looping and control statements to perform 'reduce' and 'lookup' actions:
For reduce: Initialize variable(s) (array) first and push the lookup value to the array(s):
Since the input for the next action requires options that are readable to the user we have to use the channel name.
For lookup: Loop through the original object array and compare each object channel name to your query value:
The user response is stored in selectedResponse. We can loop through each item from the original list channels response and compare this to each objects channel name. If there is a match. We can use the current items id as the argument to the "Post your own adaptive card" action.
Result:

Related

How do I select whether the routine continues based on the participant's response?

I want to create an experiment in PsychoPy Builder that conditionally shows a second routine to participants based on their keyboard response.
In the task, I have a loop that first goes through a routine where participants have three options to respond ('left','right','down') and only if they select 'left', regardless of the correct answer, should they see a second routine that asks a follow-up question to respond to. The loop should then restart with routine 1 each time.
I've tried using bits of code in the "begin experiment" section as such:
if response.key=='left':
continueRoutine=True
elif response.key!='left':
continueRoutine=False
But here I get an error saying response.key is not defined.
Assuming your keyboard component is actually called response, the attribute you are looking for is called response.keys. It is pluralised as it returns a list rather than a single value. This is because it is capable of storing multiple keypresses. Even if you only specify a single response, it will still be returned as a list containing just that single response (e.g. ['left'] rather than 'left'). So you either need to extract just one element from that list (e.g. response.keys[0]) and test against that, or use a construction like if 'left' in response.keys to check inside the list.
Secondly, you don't need to have a check that assigns True to continueRoutine, as it defaults to being True at the beginning of a routine. So it is only setting it to False that results in any action. So you could simply do something like this:
if not 'left' in response.keys:
continueRoutine = False
Lastly, for PsychoPy-specific questions, you might get better support via the dedicated forum at https://discourse.psychopy.org as it allows for more to-and-fro discussion than the single question/answer structure here at SO.

Undefined index error if any element is empty

I am sending 5 input values on single click in angularjs. Data is send as objects of object. For example
Object{0:Object, 1:object, 2:Object .., 5:Object}.
Every object have Two inputs say first name and last name. User can send any input pair.
I am sending this as post request. Now on Backend side, I m using Laravel. I m storing this object in a variable let say
`$x=$request->input('data');
I loop it for every object it will create new record.
Problem I am facing:
If all objects is sends then it properly saving the data in database.
But if one of the object is empty, Empty means if use enter 2nd input and 3rd input only and leave blank the 1st, 3rd and 5th input.
Then laravel throwing error of undefined index as on first object it not receiving any firstname or lastname for 1st request. SO how can I solve this problem.I wanted that it should update the fields whichever it receives.
It seems you need to filter out empty objects before persisting.
$data = $request->input('data');
$dataToSave = [];
foreach ($data as $value) {
if (!empty($value)) {
$dataToSave[] = $data;
}
}
// persist $dataToSave with your logic
Its better to change your firstname and lastname to nullable in order to accept null values
check the input values in ajax function and pass those key values only

Performance map and reject VS reject while mapping

Say I have an array of objects where each object responds to the method id. Let's call it arr_to_proces And I have another array of ids. Let's call ids_to_not_consider
I want to get a set of ids from the arr_to_proces, rejecting the ones that are within the ids_to_not_consider array.
My first approach is this:
arr_to_proces.map(&:id) - ids_to_not_consider
So, I map and then I substract. Is there a more performant way of doing this in Ruby?

In Meteor Collections, use an array-field or another collection?

The question
In short, my question is: when an array in a document is changed, will the users receive the new array, or just the changes?
If that question is unclear, I've described my problem below.
The problem
I have a collection whose documents contain an array field two users will push values to. A document in this collection kind of looks like this:
var document = {
userId1: "...user id...", // The id of the first of the two users.
userId2: "...user id...", // The id of the second of the two users.
data: [] // The field the two users will push values to.
}
data will from the beginning be empty, and the users will then take turns pushing values to it.
When one of the user pushes some value to data, the server will send the changes to the second user. Will the second user receive the entire data-array, or just the changes (the pushed value)? I'm a little bit worried that the second user will receive the entire data-array, even though it's just a single value that's been pushed to it, and if data contains many values, I fear this will become a bottleneck.
Is this the case? If it is, using another collection for storing the values will solve it, right? Something like this:
var document = {
id: "...unique id...",
userId1: "...user id...", // The id of the first of the two users.
userId2: "...user id..." // The id of the second of the two users.
}
var documentData = {
idReference: "...the unique id in the document above...",
value: "...a value..."
}
Instead of pushing the values into an array in document, insert them into a collection containing documentData. This (I know) won't have the downside I fear the first solution has (but I rather use the first solution if it doesn't have the downside).
As per https://github.com/meteor/meteor/blob/master/packages/livedata/DDP.md
changed (server -> client):
collection: string (collection name)
id: string (document ID)
fields: optional object with EJSON values
cleared: optional array of strings (field names to delete)
Users will receive the new array. To only send "diffs," use a collection of {userId: userId, value: value} documents.
I inspected what was sent as commented by user728291, and it seems like the entire array-field is sent, and not just the pushed value. I don't know if this always is the case (I just tested with an array containing few and small values; if it contains many or big values Meteor maybe try to do some optimization I couldn't see in my tiny test), but I'll go with the solution using another collection instead of the array-field.

Pass array as parameter to JAX-RS resource

I have many parameters to pass to the server using JAX-RS.
Is there a way to pass or AarryList with the URL?
You have a few options here.
Option 1: A query parameter with multiple values
You can supply multiple simple values for a single query parameter. For example, your query string might look like:
PUT /path/to/my/resource?param1=value1&param1=value2&param1=value3
Here the request parameter param1 has three values, and the container will give you access to all three values as an array (See Query string structure).
Option 2: Supply complex data in the PUT body
If you need to submit complex data in a PUT request, this is typically done by supplying that content in the request body. Of course, this payload can be xml (and bound via JAXB).
Remember the point of the URI is to identify a resource (RFC 3986, 3.4), and if this array of values is data that is needed to identify a resource then the URI is a good place for this. If on the other hand this array of data forms part of the new representation that is being submitted in this PUT request, then it belongs in the request body.
Having said that, unless you really do just need an array of simple values, I'd recommend choosing the Option 2. I can't think of a good reason to use URL-encoded XML in the URL, but I'd be interested to hear more about exactly what this data is.
We can get the Query parameters and corresponding values as a Map,
#GET
#Produces(MediaType.APPLICATION_JSON)
public void test(#Context UriInfo ui) {
MultivaluedMap<String, String> map = ui.getQueryParameters();
String name = map.getFirst("name");
String age = map.getFirst("age");
System.out.println(name);
System.out.println(age);
}

Resources