Select2 v4 relies on <select> tags instead of hidden <input> tags so it's now easier to get the selected option or options: you just refer to the original <select>. This may have also been the case with Select2 v3 but I've only used Select2 v4.
$('#example option:selected').attr('data-id')
jsfiddle demonstration
See also Get selected value in dropdown list using JavaScript?
Edit: I like this answer for general purpose data object access:
var d = $("#dropdown").select2("data");
d will be an array containing the selected item(s) as objects, so to access the id property of the first item:
var id = d[0].id;