AlexR
AlexR

Reputation: 5634

Create object with the name a property set to the contents of variable

I am using the following code snippet to create a class payload:

function setPointer(parent, attribute, children) {
    var url = 'https://api.xzy.com/1/classes/' + parent;
    var pointToObjects = [];

    for (var i = 0; i < children.length; i++) {
        var pointToObject = {
            "__type":"Pointer",
            "className": childrenClass,
            "objectId":children[i]
        };
        pointToObjects.push(pointToObject);
    }

    var object = {
        attribute:{
            "__op":"AddRelation",
            "objects":pointToObjects
        }
    };
    var payload = JSON.stringify(object);
    var payload = {
        attribute:{
            "__op":"AddRelation",
            "objects":children
        }
    };

    var options = {
        'method': 'post',
        'contentType':'application/json',
        'headers': makeHeaders(),
        'payload': payload,
        'muteHttpExceptions':false
    };

    var jsonResponse = UrlFetchApp.fetch(url, options);
    Logger.log(jsonResponse.getResponseCode() + ': ' + jsonResponse);
}

How can I set the attribute and children properties of the payload object to the contents of the parameters attribute (e.g. "period") and children (e.g. "hgssgs", "eejss")?

I would like to create a HTTP request like the following:

curl -X PUT \
  -H "X-Parse-Application-Id: yyy" \
  -H "X-Parse-REST-API-Key: xxxx" \
  -H "Content-Type: application/json" \
  -d '{"opponents":{"__op":"AddRelation","objects":[{"__type":"Pointer","className":"Player","objectId":"Vx4nudeWn"}]}}' \
  https://api.parse.com/1/classes/GameScore/Ed1nuqPvcm

Upvotes: 0

Views: 57

Answers (1)

vz_
vz_

Reputation: 482

Place your attribute in the square brackets. It will automatically create new parameter for object with the name that stored in that string variable.

Here is an example:

var param = 'someParam';
var obj = {};
obj[param] = {
    val: 1
}

The result obj is as follows

{
    someParam: {
        val: 1
    }
}

Upvotes: 1

Related Questions