Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replicate a list N times in jq?

The syntax [...]*N only works for strings and does not work for lists. Also, I haven't found a built-in in the manual that does this task easily.

So I had to do something like

[range(N)|...]|add

Is there an easier (or more idiomatic) way to replicate a list in jq N times?


1 Answers

The syntax you're looking for does not exist. Your best bet would be to define a function to repeat the value for a reusable option.

def repeat($n):
    . as $val | range($n) | $val;

In your case, to create an array which repeats the contents of another array N times, you could use the above to do this:

[repeat($N)[]]

for some number $N.

like image 113
Jeff Mercado Avatar answered Feb 01 '26 21:02

Jeff Mercado



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!