从键和值数组创建对象

时间:2021-09-01 21:16:38

I've been banging my head against the wall for several hours on this and just can't seem to find a way to do this. I have an array of keys and an array of values, how can I generate an object? Input:

我一直在靠墙撞墙几个小时,似乎无法找到办法做到这一点。我有一组键和一组值,如何生成一个对象?输入:

[["key1", "key2"], ["val1", "val2"]]

Output:

{"key1": "val1", "key2": "val2"}

5 个解决方案

#1


4  

Resolved this on github:

在github上解决了这个问题:

.[0] as $keys |
.[1] as $values |
reduce range(0; $keys|length) as $i  ( {}; . + { ($keys[$i]): $values[$i] })

#2


3  

The current version of jq has a transpose filter that can be used to pair up the keys and values. You could use it to build out the result object rather easily.

当前版本的jq有一个转置过滤器,可用于配对键和值。您可以使用它来轻松构建结果对象。

transpose | reduce .[] as $pair ({}; .[$pair[0]] = $pair[1])

#3


1  

Just to be clear:

要明确一点:

(0) Abdullah Jibaly's solution is simple, direct, efficient and generic, and should work in all versions of jq;

(0)Abdullah Jibaly的解决方案简单,直接,高效且通用,并且应该适用于所有版本的jq;

(1) transpose/0 is a builtin in jq 1.5 and has been available in pre-releases since Oct 2014;

(1)transpose / 0是jq 1.5内置版本,自2014年10月起已在预发行版中发布;

(2) using transpose/0 (or zip/0 as defined above), an even shorter but still simple, fast, and generic solution to the problem is:

(2)使用transpose / 0(或如上定义的zip / 0),这个问题的更简单但更简单,快速和通用的解决方案是:

transpose | map( {(.[0]): .[1]} ) | add

Example:

$ jq 'transpose | map( {(.[0]): .[1]} ) | add'

Input:

[["k1","k2","k3"], [1,2,3] ]

Output:

{
  "k1": 1,
  "k2": 2,
  "k3": 3
}

#4


1  

Scratch this, it doesn't actually work for any array greater than size 2.

抓住这个,它实际上不适用于任何大于2的数组。

[map(.[0]) , map(.[1])] | map({(.[0]):.[1]}) | add

[map(。[0]),map(。[1])] | map({(。[0]):。[1]})|加

Welp, I thought this would be easy, having a little prolog experience... oh man. I ended up banging my head against a wall too. Don't think I'll ever use jq ever again.

韦尔普,我觉得这很容易,有一点经验......哦,伙计。我最后还把头撞在墙上。不要以为我会再次使用jq。

#5


0  

Here is a solution which uses reduce with a state object holding an iteration index and a result object. It iterates over the keys in .[0] setting corresponding values in the result from .[1]

这是一个解决方案,它使用包含迭代索引和结果对象的状态对象的reduce。它迭代了键。[0]在结果中设置相应的值。[1]

  .[1] as $v
| reduce .[0][] as $k (
   {idx:0, result:{}}; .result[$k] = $v[.idx] | .idx += 1
  )
| .result

#1


4  

Resolved this on github:

在github上解决了这个问题:

.[0] as $keys |
.[1] as $values |
reduce range(0; $keys|length) as $i  ( {}; . + { ($keys[$i]): $values[$i] })

#2


3  

The current version of jq has a transpose filter that can be used to pair up the keys and values. You could use it to build out the result object rather easily.

当前版本的jq有一个转置过滤器,可用于配对键和值。您可以使用它来轻松构建结果对象。

transpose | reduce .[] as $pair ({}; .[$pair[0]] = $pair[1])

#3


1  

Just to be clear:

要明确一点:

(0) Abdullah Jibaly's solution is simple, direct, efficient and generic, and should work in all versions of jq;

(0)Abdullah Jibaly的解决方案简单,直接,高效且通用,并且应该适用于所有版本的jq;

(1) transpose/0 is a builtin in jq 1.5 and has been available in pre-releases since Oct 2014;

(1)transpose / 0是jq 1.5内置版本,自2014年10月起已在预发行版中发布;

(2) using transpose/0 (or zip/0 as defined above), an even shorter but still simple, fast, and generic solution to the problem is:

(2)使用transpose / 0(或如上定义的zip / 0),这个问题的更简单但更简单,快速和通用的解决方案是:

transpose | map( {(.[0]): .[1]} ) | add

Example:

$ jq 'transpose | map( {(.[0]): .[1]} ) | add'

Input:

[["k1","k2","k3"], [1,2,3] ]

Output:

{
  "k1": 1,
  "k2": 2,
  "k3": 3
}

#4


1  

Scratch this, it doesn't actually work for any array greater than size 2.

抓住这个,它实际上不适用于任何大于2的数组。

[map(.[0]) , map(.[1])] | map({(.[0]):.[1]}) | add

[map(。[0]),map(。[1])] | map({(。[0]):。[1]})|加

Welp, I thought this would be easy, having a little prolog experience... oh man. I ended up banging my head against a wall too. Don't think I'll ever use jq ever again.

韦尔普,我觉得这很容易,有一点经验......哦,伙计。我最后还把头撞在墙上。不要以为我会再次使用jq。

#5


0  

Here is a solution which uses reduce with a state object holding an iteration index and a result object. It iterates over the keys in .[0] setting corresponding values in the result from .[1]

这是一个解决方案,它使用包含迭代索引和结果对象的状态对象的reduce。它迭代了键。[0]在结果中设置相应的值。[1]

  .[1] as $v
| reduce .[0][] as $k (
   {idx:0, result:{}}; .result[$k] = $v[.idx] | .idx += 1
  )
| .result