I'm building a drop down list using jQuery from some JSON, i'm taking the full data for this item and adding it to data-obj which i'm using later in a POST request. I'm needing to insert a piece of information into the data but cannot work out how to do it.
The data when i receive it from the server looks like this:
{
"genreId": 1,
"genre": "Horror",
"genreGroup": "Fiction",
"Publishers": [{
"publisher": "Random House",
"books": [{
"book": "The Shining",
"releaseyears": [2012, 2013, 2014]
}]
}, {
"publisher": "Penguin",
"books": [{
"book": "Dracula",
"releaseyears": [2010]
}, {
"book": "Taking Lives",
"releaseyears": [2013, 2014]
}]
}]
}
I need to add a derived field 'Original Genre' into the data so it looks like:
{
"genreId": 1,
"genre": "Horror",
"genreGroup": "Fiction",
"Publishers": [{
"publisher": "Random House",
"originalgenre": "Horror",
"books": [{
"book": "The Shining",
"releaseyears": [2012, 2013, 2014]
}]
}, {
"publisher": "Penguin",
"originalgenre": "Horror",
"books": [{
"book": "Dracula",
"releaseyears": [2010]
}, {
"book": "Taking Lives",
"releaseyears": [2013, 2014]
}]
}]
}
I think the relevant code is:
$(info.genre).each(function (i) {
var bookitem = $('<a/>', {
text: this.genre,
'data-value': this.genre,
'class': 'selectable',
'data-name': "G" + (this.genre),
'data-obj': JSON.stringify(this),
'data-target': 'booksection'
});
listGenres.append($("<li/>").append(bookitem));
[....do other things ...]
So it's when i add the data-obj element i get the data available (just as it is in the first code block, i need to do amend it before i build the list to add the extra field in.
Any ideas how i can achieve this?