title: How do I remove elements from array in a JSON object containing a value
body: <p><strong>Please note that this post is tagged <code>jq</code>, I am not trying to do this with javascript at all.</strong></p>
  
  <p>I have an object that has an array as one of its fields. I'd like to remove the elements from the object's array that match a criteria, but keep the object around. I've looked around and I've only been able to spot snippets of returning <em>just</em> the array that happens to have less items now.</p>
  
  <p>Here's an example:</p>
  
  <pre><code>{
    "foo": "bar",
    "events": [
      {
        "id": 1,
        "msg": "starting"
      },
      {
        "id": 2,
        "msg": "working on it"
      },
      {
        "id": null,
        "msg": "done"
      }
    ]
  }
  </code></pre>
  
  <p>and I'd like to get back the following:</p>
  
  <pre><code>{
    "foo": "bar",
    "events": [
      {
        "id": 1,
        "msg": "starting"
      },
      {
        "id": 2,
        "msg": "working on it"
      }
    ]
  }
  </code></pre>
  
  <p>Note: Just in case this will affect the answer I'm really only interested in the last element. I plan to build up a new object in a following pipe stage like:</p>
  
  <pre><code>jq '$answer | {bar:.foo, event: .events[-1].msg}`
  </code></pre>
  
  <p>I have tried both</p>
  
  <pre><code>select(.events[].id != null)
  </code></pre>
  
  <p>and</p>
  
  <pre><code>del(.events[], select(.id == null)))
  </code></pre>
  
  <p>with no luck.</p>