如何为数组中的嵌套数组中的多个对象生成持续索引

时间:2022-08-22 20:34:36

Given

鉴于

[{
  "objects": [{
    "key": "value"
  },{
    "key": "value"
  }]
}, {
  "objects": [{
    "key": "value"
  }, {
    "key": "value"
  }]
}]

How do I generate

我怎么生成

[{
  "objects": [{
    "id": 0,
    "key": "value"
  },{
    "id": 1,
    "key": "value"
  }]
}, {
  "objects": [{
    "id": 2,
    "key": "value"
  }, {
    "id": 3,
    "key": "value"
  }]
}]

Using jq?

使用金桥?

I tried to use this one, but ids are all 0:

我试着用这个,但是id都是0:

jq '[(-1) as $i | .[] | {objects: [.objects[] | {id: ($i + 1 as $i | $i), key}]}]'

2 个解决方案

#1


1  

The key to a simple solution here is to break the problem down into easy pieces. This can be accomplished by defining a helper function, addId/1. Once that is done, the rest is straightforward:

一个简单解决方案的关键是将问题分解成简单的部分。这可以通过定义辅助函数addId/1来实现。一旦这样做了,剩下的就很简单了:

# starting at start, add {id: ID} to each object in the input array 
def addId(start):
  reduce .[] as $o
    ([];
     length as $l 
     | .[length] = ($o | (.id = start + $l)));

reduce .[] as $o
  ( {start: -1, answer: []};
    (.start + 1) as $next
    | .answer += [$o | (.objects |= addId($next))]
    | .start += ($o.objects | length) )
| .answer

#2


0  

Inspired by @peak answer, I came up with this solution. Not much difference, just shorter way to generate IDs and opt for foreach instead of reduce since there is intermediate result involved.

受@peak回答的启发,我想出了这个解决方案。没有太大的差别,只是更短的生成id的方法,并选择foreach而不是reduce,因为其中包含了中间结果。

def addIdsStartWith($start):
    [to_entries | map((.value.id = .key + $start) | .value)];

[foreach .[] as $set (
    {start: 0};
    .set = $set |
        .start as $start | .set.objects |= addIdsStartWith($start) |
        .start += ($set.objects | length);
    .set
)]

#1


1  

The key to a simple solution here is to break the problem down into easy pieces. This can be accomplished by defining a helper function, addId/1. Once that is done, the rest is straightforward:

一个简单解决方案的关键是将问题分解成简单的部分。这可以通过定义辅助函数addId/1来实现。一旦这样做了,剩下的就很简单了:

# starting at start, add {id: ID} to each object in the input array 
def addId(start):
  reduce .[] as $o
    ([];
     length as $l 
     | .[length] = ($o | (.id = start + $l)));

reduce .[] as $o
  ( {start: -1, answer: []};
    (.start + 1) as $next
    | .answer += [$o | (.objects |= addId($next))]
    | .start += ($o.objects | length) )
| .answer

#2


0  

Inspired by @peak answer, I came up with this solution. Not much difference, just shorter way to generate IDs and opt for foreach instead of reduce since there is intermediate result involved.

受@peak回答的启发,我想出了这个解决方案。没有太大的差别,只是更短的生成id的方法,并选择foreach而不是reduce,因为其中包含了中间结果。

def addIdsStartWith($start):
    [to_entries | map((.value.id = .key + $start) | .value)];

[foreach .[] as $set (
    {start: 0};
    .set = $set |
        .start as $start | .set.objects |= addIdsStartWith($start) |
        .start += ($set.objects | length);
    .set
)]