<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to add to an array in Cloud Code?]]></title><description><![CDATA[<p dir="auto">I'm 100% not sure how to add a string to an array of strings. Nothing is hard coded so I can't just write a raw array, I need to reference two different variables and add them together and I can't seem to figure it out.</p>
<p dir="auto">In GameSparks it was as easy as just calling push, but either I get an error or I end up with a number as a result.<br />
For example if I was to try and just push the string "purchaseToAdd" the result is an integer which is the length of the array and not the actual array itself. There's nothing in the API for this relatively simple function.</p>
<pre><code>function GetPurchases(purchaseToAdd)
{
var entityType = "Purchases";
var entityProxy = bridge.getEntityServiceProxy();
var newPurch = [];
newPurch = newPurch.push(purchaseToAdd);

var postResult = entityProxy.getSingleton(entityType);

    if (postResult.status == 200) 
    {
        var entId = postResult.data.entityId;
        var jsonData = [];
        
         for (var i = 0; i &lt; postResult.data.data.purchases.length; i++) 
         {
             jsonData = jsonData.push(postResult.data.data.purchases[i]);
         }
        
        //jsonData = Array.prototype.slice.call(postResult.data.data.purchases);
        jsonData = jsonData.push(newPurch);
        
        var jsonEntityData = {
    "purchases": Array.prototype.slice.call(jsonData)
};
</code></pre>
]]></description><link>https://forums.getbraincloud.com/topic/145/how-to-add-to-an-array-in-cloud-code</link><generator>RSS for Node</generator><lastBuildDate>Mon, 18 May 2026 07:17:47 GMT</lastBuildDate><atom:link href="https://forums.getbraincloud.com/topic/145.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 06 Mar 2022 19:21:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to add to an array in Cloud Code? on Sat, 19 Mar 2022 22:18:10 GMT]]></title><description><![CDATA[<p dir="auto">I posted this almost 13 days ago with zero answers, so I took some time to look through a bunch of documentation and did a ton of trial and error. Please Braincloud for the love of god add this to your API.</p>
<p dir="auto"><em><strong>Rhino does not handle Arrays the way that other coding languages do.</strong></em></p>
<p dir="auto">I figured it out, unlike most arrays that return an array after you call push- Rhino returns the new length. So if you call push, don't set it's value.</p>
<pre><code>var copyArray = []; //I assume this line tells it to be an array

for (var i = 0; i &lt; postResult.data..length; i++) 
         {
             copyArray[i] = postResult.data[i]; 
//arrays do not behave like .Net arrays, you can change length without initializing, so you just set each index to the index of the array you're copying
         }

copyArray.push(variableToAdd); 
//Do NOT set this as the value of your array, the push method returns an int (length of array) NOT an array!
//you probably dont need to call push at all to add to the array as shown in the for loop above if you already know your length or want a specific length

</code></pre>
<p dir="auto">Then just set your value to the variable, like this</p>
<pre><code>  var entityData = {
    "variableName": copyArray
};
//No need to call Array(copyArray) or any of the prototype. slice stuff. If you call Array() you'll just nest the array a second time. 
</code></pre>
<p dir="auto">Use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array" rel="nofollow ugc">this page</a> for further info on functions you can do with arrays.</p>
]]></description><link>https://forums.getbraincloud.com/post/440</link><guid isPermaLink="true">https://forums.getbraincloud.com/post/440</guid><dc:creator><![CDATA[helliyum]]></dc:creator><pubDate>Sat, 19 Mar 2022 22:18:10 GMT</pubDate></item></channel></rss>