在 KRL 中迭代哈希的最佳方法是什么?

发布于 2024-10-24 07:59:44 字数 562 浏览 3 评论 0原文

假设我有一个哈希,但我不知道哈希的内容(所以我不能为此使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹倦 2024-10-31 07:59:44

迈克,在影响之后,我将抽出时间为哈希构建一个keys()运算符。

与此同时,我为解决这个问题所做的就是保留一个单独的键数组。这样,我可以在索引上使用映射、过滤器和所有设置操作,然后使用这些值作为哈希操作的键。

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

这实际上仅在您控制中的值时才有效。哈希,但这里有一些示例代码:

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);
}

注意到我使用了集合运算符 union 让数组充满唯一值

生成的 javascript 显示了它的作用:

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'};

现在,您可以使用 地图过滤运算符将每个值单独传递给函数

c.map(function(x){x+2})

c.filter(function(x){x<5})

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

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

This really only works if you are controlling the values in the hash, but here is some example 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 operator union to keep the array full of unique values

The javascript that is generated shows what this 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 the map or filter operators to pass each value individually to a function

c.map(function(x){x+2})

c.filter(function(x){x<5})
娇妻 2024-10-31 07:59:44

我建议这样做:

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

请参阅 http://docs.kynetx.com/docs/Select

I'll suggest this:

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

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

羞稚 2024-10-31 07:59:44

我相信我已经解决了这个问题,但答案有点破解。

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()
}

此递归函数迭代名称数组,该数组包含哈希中的键,并删除它在每次迭代中处理的键。

要调用此函数,只需调用:

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

对于那些不熟悉数组的 head() 和 tail() 方法的人: head() 给出数组中的第一个元素,而 tail() 给出一个删除了第一个元素的新数组。

我不太喜欢这个解决方案,因为您必须将哈希的名称或键作为参数之一传递到函数中。截至撰写本文时,我不知道有什么方法可以从哈希中提取密钥,或者我只会使用它。

I believe I've figured this out, but the answer is a little bit of a hack.

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 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:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文