jq unique_by - Choose remaining element
<p>I'm currently trying to get a list of incidents from PagerDuty via the REST api, <a href="https://v1.developer.pagerduty.com/documentation/rest/incidents/list" rel="nofollow noreferrer">which returns them in JSON array</a>.  I want to remove any duplicate events by using unique_by() on the incident_key. However, I want the first occurrence of the incident_key, and unique_by() is removing all but the last. Right now, if I have incident_number 849, 850, and 851, all with the same incident_key, unique_by() will return 851.</p>

<p>Simple example:</p>

<pre><code>[
{ "reference_key":"200", "id":"1" },
{ "reference_key":"200", "id":"2" },
{ "reference_key":"200", "id":"3" },
{ "reference_key":"201", "id":"4" },
{ "reference_key":"201", "id":"5" },
{ "reference_key":"201", "id":"6" }
]
</code></pre>

<p>What I'm trying to do is use unique_by() to get the first occurrence of reference_key, based on the id. So in this case, I'd want the output to be</p>

<pre><code>[
{ "reference_key":"200", "id":"1" },
{ "reference_key":"201", "id":"4" }
]
</code></pre>

<p>The problem is that I have no control over this, and with the data I'm currently trying to do this with, it is returning the last occurrence instead of the first, like so.</p>

<pre><code>[
{ "reference_key":"200", "id":"3" },
{ "reference_key":"201", "id":"6" }
]
</code></pre>

<p>I have tried using reverse and then calling unique_by(), but I am getting the same results. Is there any way to have some control over this?</p>