I have a simple array as such :
[1,2,3,4,5]
and I wish to get a range for example from index 2 to index 4.
I can get the last element of the array by :
json.get key .[-1]
However I cannot find anyway to return a specific results without trimming the array.
Is there anyway I can get a range of elements from a Redis json array?
The answer is :
await Client.json.get("key" , {path : '$.[1:3]'})
which gets index 1 to 3.
Related
I have a list of array id but when I select any data I want the id of the selected data, I tried using map but it gives me a list of id's in a array. Please let me know how to do this, thanks in advance
Could you provide an example in order to understand your question better?
map() will always return an array, if you are getting an array with the desired value and a long list of undefined, then just add
.filter(e => e)
at the end.
If you want to iterate and obtain a single value, reduce() might be what you are looking for.
Greetings StackOverflow!
Suppose I have an Elm app with a variable number of text input fields. I'd like to reflect the state of these input fields in the model.
Model and view are easy enough: View just has a Array String field in it somewhere.
The view is then computed simply by calling List.map (HTML input ...) on that list of strings.
However, I'm a bit lost how to do the update function and message type.
The message could be somethign like this:
type Msg = InputFieldUpdated Int String
Here, the Int refers to the position in the Array that the string to be updated has. However, if I do it this way, I can create messages that refer to non-existant array positions simply by setting the Int to something that is out of range.
For a fixed number of input elements, one can solve this problem very elegantly by simply using a union type with a different value for each input, but what about my situation? In the domain of "making impossible states impossible", is there some trick for that that I'm missing?
However, if I do it this way, I can create messages that refer to non-existant array positions simply by setting the Int to something that is out of range.
According to the documentation of Array.set, the array stays untouched if the index is out of range.
Please, have a look at this ellie-app example. It mostly models the problem, that you've described. Array of elements is rendered and the elements can be added dynamically:
view : Model -> Html Msg
view model =
div []
[ div [] (toList (Array.indexedMap viewElement model.elements))
, button [ onClick Add ] [ text "Add" ]
]
In order to update a particular element, you need to get it by index. Since the type of the result from Array.get is Maybe a, type system will force you to process all the cases (when the element exists and doesn't):
Increment index ->
case get index model.elements of
Just element ->
{ model | elements = set index (element + 1) model.elements }
Nothing ->
model
The example is just for demonstration purposes, if you don't need the current element, then Array.set function can be safely used.
I'm trying to initialise a set of elements with a unique identifier. The elements are part of the initial model.
What I have in mind is a function
initElement: Int -> InputElement -> Output
initElement id element = ...
that adds the given id to the given element. Now I'm iterating over these elements in another function like this:
uid = 0
elementsList = ...
newList = List.map (initElement uid++) elementsList
and I would want this integer uid increased with every iteration over an element by List.map, so that every element gets a unique number assigned. The ++ obviously doesn't work though.
Is this possible, or am I thinking too object-oriented? I'm pretty new to functional programming.
One option is to use List.indexedMap
http://package.elm-lang.org/packages/elm-lang/core/3.0.0/List#indexedMap
It gives you a nice incrementing index as a parameter to your function as first parameter. Which you can then use to modify the incoming uid approriattely
Using int rand=((arc4random()%4)+1);
how would you make a sorted list based on the numbers generated which have to be clicked in order .Example if 4 2,3,1 was generated you have to click images based on those numbers ?
You could keep those integers [x,y,z] in a mutable array. and if you want the user to click x then y then z (indexing ascending order) then you could check the object at index 0 and see if it matches your image (I'm assuming you can map an integer to your image somehow). If it does, remove the object at index 0 from the list, if it doesn't, tell the user they have failed.
When the list is empty you know they have succeeded. you should check this after ever time your remove index 0.
How can we avoid multiple iteration to search for an object's property and if found then assign it to a variable else search for another key ?
eq we have Video Class with one of the field as videoType which can have values as hq(high-quality),normal(normal), def(default)..etc and so on.
From an array containing multiple video objects, how can we search and return a particular object in an order that if the array contains object with property hq then first return it,else search for normal and proceed so on. if a set of n keys are to be tested in the key set (hq,normal,def,....) then do we always need to iterate the entire array "n" times unless the key is found.
Can this be done is single iteration ? Do we need to first sort the original array in the order of occurrence of the keys in desired key set. I hope my problem statement is clear.
One possible solution for this would be to create separate NSMutableArrays for each videoType. Then, as you iterate once over your array of video objects, you check its array type and add the video to the correct array.
After you finished iterating, you create the final mutable array by concatenating the other array with addObjectsFromArray.
If you have a lot or variable list of video types, you can create the separate mutable arrays as values in an NSDictionary, where the keys are the video types. This way, you can get the target array with one step, by fetching it from the dictionary.