0

I am attempting to work with an API that requires me to structure the request body in a predefined structure. It's full of multi layered hierarchy and I'm unsure on what's the best way to go about creating such a data structure.

The code below works, but it doesn't look conventially tidy. What is the general consensus on how to go about this?

For reference, I am making a call to the following API: https://cloud.google.com/datastore/docs/reference/data/rest/v1/projects/commit

var transaction = {
    transaction: transactionId,
    mode: 'TRANSACTIONAL',
    mutations: []
};

var entity = {
  key: {
    kind: kind
  },
  properties: {}
};

if (args.entityId) {
  entity.key.id = args.entityId;
}

for (var property in args) {
  if (args[property].dataType) {
    var propertyType = args[property].dataType + 'Value';
    entity.properties[property] = {};
    entity.properties[property][propertyType] = args[property].value;
  }
}

var mutation  = new Object();
mutation[operationType] = entity; 

transaction.mutations.push(mutation)

If I remove entity.properties[property] = {} my code errors, but that line does just not sit well with me.

Oh, and it is important to mention that I am confined to JavaScript 1.6 since I am working with Google Apps Script.

Your time in answering this is greatly appreciated.

neelcor
  • 23
  • 3
  • 1
    You aren't explicitly limited to JS 1.6 - Apps Script has some niceties, namely `Object.create`, `Object.defineProperty/ies`, that could be useful. In general the most maintainable code is (to me) what you already have, as the intent is very clear, even to those who are not intimately familiar with JavaScript. Possibly useful is part of [this answer](https://stackoverflow.com/a/51732281/9337071) – tehhowch Aug 12 '18 at 16:56
  • @tehhowch Thank you! – neelcor Aug 12 '18 at 18:20

0 Answers0