I have a str'ed dictionary, i.e '{}' instead of simply {}. How to exctract data from this dict and put into a table?
Here is the dict:
requesterArcticresReservations =
'{'A20573': {'reservation_url': 'https://sst.arcticres.com/reservation/20573',
'trip_name': 'Skagway Triple Adventure',
'datetime': '8/7/16 at 12:45PM',
'visitors': '2 guys'
}}'
Here is a sketch of the code I need:
(function() {
"use strict";
return {
initialize: function() {
var requesterArcticresId = domHelper.ticket.getContactInfo().user.custom_field.cf_arcticres_person_id;
var requesterArcticresReservations = domHelper.ticket.getContactInfo().user.custom_field.cf_arcticres_reservations;
var reservations_html;
if (requesterArcticresId) {
var d = JSON.parse(requesterArcticresReservations);
// what to write here in order to add data to the table?
reservations_html =+
'<table><tr>' +
'<td><a' + reservation_url + '>' + id + '</a></td>' +
'<td>' + trip_name + '<br>' + visitors + '<br>' + datetime '</td>' +
'</tr></table>';
jQuery(this.$container).find('#apptext').html(reservations_html);
}
else {
jQuery(this.$container).find('#apptext').text("There is no data to show");
}
appPlaceholder.ticket.belowRequestorInfo(jQuery(this.$container));
}
};
})();
in Python I can exctract from dict all values and add them to the table, something like this:
reservations_html = ''
for id in dic:
reservation_url = reservation_url['reservation_url']
trip_name = id['trip_name']
visitors = id['visitors']
date_n_time = id['datetime']
reservations_html =+
'<table><tr>' +
'<td><a' + reservation_url + '>' + id + '</a></td>' +
'<td>' + trip_name + '<br>' + visitors + '<br>' + datetime '</td>' +
'</tr></table>';
How can I do the same in javascript?
FINAL WORKABLE VERSION
(function() {
"use strict";
return {
initialize: function() {
var requesterArcticresId = domHelper.ticket.getContactInfo().user.custom_field.cf_arcticres_person_id;
var requesterArcticresReservations = domHelper.ticket.getContactInfo().user.custom_field.cf_arcticres_reservations;
var reservations_html;
if (requesterArcticresId) {
var d = JSON.parse(requesterArcticresReservations);
for (var id in d) {
if(d.hasOwnProperty(id)) {
var reservation_url = d[id]['reservation_url'];
var trip_name = d[id]['trip_name'];
var visitors = d[id]['visitors'];
var datetime = d[id]['datetime'];
reservations_html =+
'<table><tr>' +
'<td><a href=' + reservation_url + '>' + id + '</a></td>' +
'<td>' + trip_name + '<br>' + visitors + '<br>' + datetime + '</td>' +
'</tr></table>';
}
}
jQuery(this.$container).find('#apptext').html(reservations_html);
}
else {
jQuery(this.$container).find('#apptext').text("There is no data to show");
}
appPlaceholder.ticket.belowRequestorInfo(jQuery(this.$container));
}
};
})();
Thanks to @Felix Kling that showed me the right direction!
P.S. Found, why JSON.parser
worked - I manually changes all keys\values from '
to "
in admin panel.