0

I am having array in below format. i am having issue to append the value inside each chapter array. i need to add field like 'checked:true'. I need to get the data in same format. Expectation is, need same format with added one field inside chapter array.

{ 
  "Books": [
    {
      "label":"Book1",
      "data": [
        {
          "bookId": 3561,
          "bookName": "AJ200",
          "chapters": [
            {
              "id": 3926,
              "name": "red"
            },
            {
              "id": 3927,
              "name": "yellow"
            },
            {
              "id": 3928,
              "name": "black"
            }
          ]
        }
      ]
    },
    {
      "label":"Book2",
      "data":[
        {
          "bookId": 3561,
          "bookName": "AJ200",
          "chapters": [
            {
              "id": 3564,
              "name": "blue"
            },
            {
              "id": 3565,
              "name": "orange"
            }
          ]
        }
      ]
    }
  ]
}

after adding field chapters array looks like as below,

"chapters": [
            {
              "id": 3564,
              "name": "blue",
              "checked":true
            },
            {
              "id": 3565,
              "name": "orange"
              "checked":true
            }
2
  • 1
    How are you adding it, post the code Commented Nov 22, 2016 at 10:40
  • You just need to iterate and add the checked property. Array.forEach will suffice Commented Nov 22, 2016 at 10:42

1 Answer 1

1

This is a little verbose but works all the same!

var myObject = 
{ 
  "Books": [
    {
      "label":"Book1",
      "data": [
        {
          "bookId": 3561,
          "bookName": "AJ200",
          "chapters": [
            {
              "id": 3926,
              "name": "red"
            },
            {
              "id": 3927,
              "name": "yellow"
            },
            {
              "id": 3928,
              "name": "black"
            }
          ]
        }
      ]
    },
    {
      "label":"Book2",
      "data":[
        {
          "bookId": 3561,
          "bookName": "AJ200",
          "chapters": [
            {
              "id": 3564,
              "name": "blue"
            },
            {
              "id": 3565,
              "name": "orange"
            }
          ]
        }
      ]
    }
  ]
}

var books = myObject.Books;


for(var i=0; i<books.length; i++) {

  var bookData = books[i].data;

  for(var j=0;j<bookData.length;j++) {

    var chapters = bookData[j].chapters;

    for(var k=0;k<chapters.length;k++) {

      chapters[k].checked = true;

    }

  }

}

console.log(JSON.stringify(books));
Sign up to request clarification or add additional context in comments.

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.