Pushing array to Firebase via REST API
<h1>Question</h1>

<blockquote>
  <p>How do I (with a <strong>single HTTP request</strong> to the <strong>REST API</strong>) write an <strong>array</strong> to Firebase and give each array element a (non-integer) <strong>unique ID</strong>?</p>
</blockquote>

<p><a href="https://www.firebase.com/docs/web/guide/saving-data.html#section-push" rel="nofollow">As described here</a>.</p>

<p><hr></p>

<h2>Data</h2>

<p>The data I have to write looks like the following.</p>

data-to-write.js

<pre><code>myArray = [ {"user_id": "jack", "text": "Ahoy!"},
            {"user_id": "jill", "text": "Ohai!"} ];
</code></pre>

<h2>Goal</h2>

<p>When finished, I want my Firebase to look like this following.</p>

my-firebase.firebaseio.com

<pre><code>{
  "posts": {
    "-JRHTHaIs-jNPLXOQivY": { // &lt;- unique ID (non-integer)
      "user_id": "jack",
      "text": "Ahoy!"
    },
    "-JRHTHaKuITFIhnj02kE": { // &lt;- unique ID (non-integer)
      "user_id": "jill",
      "text": "Ohai!"
    }
  }
}
</code></pre>

<p>I <strong>do not</strong> want it to look like this following...</p>

my-anti-firebase.firebaseio.com

<pre><code>// NOT RECOMMENDED - use push() instead!
{
  "posts": {
    "0": {  // &lt;- ordered array index (integer)
      "user_id": "jack",
      "text": "Ahoy!"
    },
    "1": {  // &lt;- ordered array index (integer)
      "user_id": "jill",
      "text": "Ohai!"
    }
  }
}
</code></pre>

<p><a href="https://www.firebase.com/blog/2014-04-28-best-practices-arrays-in-firebase.html" rel="nofollow">I note this page where it says</a>:</p>

<blockquote>
  <p>[...] if all of the keys are integers, and more than half of the keys between 0 and the maximum key in the object have non-empty values, then Firebase will render it as an array.</p>
</blockquote>

<h2>Code</h2>

<p>Because I want to do this in a single HTTP request, I want to avoid iterating over each element in the array and, instead, I want to push a batch in a single request.</p>

<p>In other words, I want to do something like this:</p>

pseudocode.js

<pre><code>curl -X POST -d '[{"user_id": "jack", "text": "Ahoy!"},
                  {"user_id": "jill", "text": "Ohai!"}]' \
                  // I want some type of batch operation here
  'https://my-firebase.firebaseio.com/posts.json'
</code></pre>

<p>However, when I do this, I get exactly what I describe above that I don't want (i.e., sequential integer keys).</p>

<p>I want to avoid doing something like this:</p>

anti-pseudocode.js

<pre><code>for(i=0; i&lt;=myArray.length; i++;){ // I want to avoid iterating over myArray
  curl -X POST -d '{"user_id": myArray[i]["user_id"],
                    "text": myArray[i]["text"]}' \
    'https://my-firebase.firebaseio.com/posts.json'
}
</code></pre>

<p>Is it possible to accomplish what I have described? If so, how?</p>