What is the best way to iterate over a hash in KRL?

Let's say I have a hash where I don't know the contents of the hash (so I can't use the selection for this). Here is an example hash:

{ "key1" : "value1", "key2" : "value2", "key3" : "value3" } 

I want to iterate over this hash and create the following array:

 ["key1=value1", "key2=value2", "key3=value3"] 

My first approach would be to create a recursive function that iterates through the hash and populates the array, but I'm not sure if this can be done. In the array, I can use head () and tail () to help with recursion, but these operators are not available for the hash (as far as I know).

I want to initiate this from within a function, because I am doing this in a module. For instance:

 hash_to_array = function(h) { // magic code here } manipulate_params = function(params) { params_array = hash_to_array(params); // more code here... } 
+4
source share
3 answers

Mike, after the hit, I'll make time to create a keys () statement for the hashes.

In the meantime, what I did to get around this is to keep a separate array of keys. That way, I can use the map, filter, and all installed operations on the index, and then use these values ​​as my keys for hash operations

 key_array = ["key1","key2","key3"]; my_hash = { "key1" : "value1", "key2" : "value2", "key3" : "value3" }; 

This really only works if you control the values ​​in the hash, but here is a sample code:

 global { kvHash = { "key1" : "value1", "key2" : "value2", "key3" : "value3" }; kArray = ["key1","key2","key3"]; } pre { pickKey = kArray[1]; value = kvHash.pick("$.#{pickKey}"); // add a new value newKey = "key4"; newVal = "value4"; newArray = kArray.union(newKey); newHash = kvHash.put([newKey],newVal); } 

Noticed that I used the set union operator so that the array has unique values

Created javascript shows what it does:

 var pickKey = 'key2'; var value = 'value2'; var newKey = 'key4'; var newVal = 'value4'; var newArray = ['key1', 'key2', 'key3', 'key4']; var newHash = {'key2' :'value2','key1' :'value1','key4' :'value4','key3' :'value3'}; 

Now you can use map or filter to pass each value individually to a function

 c.map(function(x){x+2}) c.filter(function(x){x<5}) 
+3
source

I suggest the following:

 foreach my_hash setting(key, value) pre { my_array.push("#{key}=#{value}"); } 

See http://docs.kynetx.com/docs/Select

+2
source

I believe I understood this, but the answer is a bit hacked.

 hash_to_sorted_array = function(params, names, new_a) { n = names.head(); val = params.pick("$.#{n}", true); appended_array = new_a.append("#{n}=#{val.head()}"); finished_array = (names.length() == 0) => new_a | hash_to_sorted_array(params, names.tail(), appended_array); finished_array.sort() } 

This recursive function iterates over an array of names that holds the keys in the hash and removes the key that it processes at each iteration.

To call this function, just call:

 sorted_array = hash_to_sorted_array(params, names, []); 

For those who are not familiar with the head () and tail () methods of the array: head () gives you the first element in the array, and tail () gives you a new array with the first element removed.

I really don't like this solution because you need to pass hash names or keys to the function as one of the arguments. At the time of this writing, I don't know how to extract only keys from a hash, or I would just use that.

+2
source

Source: https://habr.com/ru/post/1344382/


All Articles