2

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?

1
  • I need to add a field at the same level as publisher. So i get the first block code from the server, i need to do something to it to add a field to it so it looks like the second block of code. Commented Jun 12, 2014 at 10:41

3 Answers 3

3
$.each(data.Publishers, function(i, v) {
    data.Publishers[i]['originalgenre'] = data.genre;
});

JS FIDDLE DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

Your variables do not match.. I !== i
You're right @Tomanow, thank you. That's the problem of writing from a phone. Corrected -- will be more careful now.
2
for (var i = 0; i < myJson.Publishers.length; i++) {
    myJson.Publishers[i].originalgenre = myJson.genre;
}

Where

var myJson = {
        "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]
                }]
        }]
};

Comments

2

I assume the "Original genre" in the Publishers array is supposed to match the "genre" in the main object? If that is the case, this should do it:

$.each(yourObject.Publishers, function(index, publisher) {
    publisher.originalgenre = yourObject.genre;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.