在 KRL 中迭代哈希的最佳方法是什么?
假设我有一个哈希,但我不知道哈希的内容(所以我不能为此使用 pick )。这是哈希的示例:
{ "key1" : "value1", "key2" : "value2", "key3" : "value3" }
我想迭代此哈希并创建以下数组:
["key1=value1", "key2=value2", "key3=value3"]
我的第一种方法是构建一个递归函数,该函数迭代哈希并填充数组,但我不确定这是否可以完成吧。在数组中,我可以使用 head() 和 tail() 来帮助递归,但这些运算符不可用于哈希(据我所知)。
我想从函数内启动此操作,因为我是在模块中执行此操作。例如:
hash_to_array = function(h) {
// magic code here
}
manipulate_params = function(params) {
params_array = hash_to_array(params);
// more code here...
}
Let's say I have a hash where I don't know the contents of the hash (so I can't use pick for this). Here is an example of the 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 build a recursive function that iterates through the hash and populates the array, but I'm not sure if that can be done. In an array, I can use head() and tail() to help with the recursion, but those operators aren't available for a hash (as far as I know).
I want to initiate this from within a function because I'm doing this in a module. For example:
hash_to_array = function(h) {
// magic code here
}
manipulate_params = function(params) {
params_array = hash_to_array(params);
// more code here...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
迈克,在影响之后,我将抽出时间为哈希构建一个keys()运算符。
与此同时,我为解决这个问题所做的就是保留一个单独的键数组。这样,我可以在索引上使用映射、过滤器和所有设置操作,然后使用这些值作为哈希操作的键。
这实际上仅在您控制中的值时才有效。哈希,但这里有一些示例代码:
注意到我使用了集合运算符 union 让数组充满唯一值
生成的 javascript 显示了它的作用:
现在,您可以使用 地图 或 过滤运算符将每个值单独传递给函数
Mike, after impact, I will carve out time to build a keys() operator for hashes.
In the meantime, what I have done to get around this is to keep a separate array of the keys. That way, I can use map, filter and all of the set operations on the index and then use the those values as my keys for the hash operations
This really only works if you are controlling the values in the hash, but here is some example code:
Noticed that I used the set operator union to keep the array full of unique values
The javascript that is generated shows what this does:
Now, you can use the map or filter operators to pass each value individually to a function
我建议这样做:
请参阅 http://docs.kynetx.com/docs/Select
I'll suggest this:
See http://docs.kynetx.com/docs/Select
我相信我已经解决了这个问题,但答案有点破解。
此递归函数迭代名称数组,该数组包含哈希中的键,并删除它在每次迭代中处理的键。
要调用此函数,只需调用:
对于那些不熟悉数组的 head() 和 tail() 方法的人: head() 给出数组中的第一个元素,而 tail() 给出一个删除了第一个元素的新数组。
我不太喜欢这个解决方案,因为您必须将哈希的名称或键作为参数之一传递到函数中。截至撰写本文时,我不知道有什么方法可以从哈希中提取密钥,或者我只会使用它。
I believe I've figured this out, but the answer is a little bit of a hack.
This recursive function iterates over the names array, which contains the keys in the hash and removes the key that it processes in each iteration.
To call this function, just call:
For those unfamiliar with the head() and tail() methods of an array: head() gives you the first element in the array while tail() gives you a new array with the first element removed.
I don't really love this solution because you have to pass the names, or the keys, of the hash into the function as one of the arguments. As of this writing, I don't know of any way to extract just the keys from a hash, or I would just use that.