0

I am building an object to pass into a chartjs line chart and I'm looking to see if I can make it a little more dynamic.

Right now I have the following when building the data object, and it works a treat:

   var inboundEvent = {
     time: $A.localizationService.formatDateTime(message.data.payload.CreatedDate, 'HH:mm'),
     createdById: message.data.payload.CreatedById,
     key: message.data.payload.key__c,
     value: message.data.payload.value__c
    } 

    //add the most recent event to the array
    events.push(inboundEvent);

I want to create variables for key__c and value__c and then use the variable instead of the literal field name that is currently being used. The __c values are hardcoded field names coming off an event stream and i'd like to change it so that I could use the same code on a different event and pass the field names from that new event in dynamically.

This isn't (or doesn't seem like) a simple string concatenation..

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
adam.sellers
  • 181
  • 5
  • 16

1 Answers1

0

Something like this

var key__c   = "YOUR_KEY_HERE_0";
var value__c = "YOUR_KEY_HERE_1";
var inboundEvent = {
  time: $A.localizationService.formatDateTime(message.data.payload.CreatedDate, 'HH:mm'),
  createdById: message.data.payload.CreatedById,
  key: message.data.payload[key__c],
  value: message.data.payload[value__c]
}
riyaz-ali
  • 8,677
  • 2
  • 22
  • 35