jeeva sr jeeva sr - 16 days ago 6
Javascript Question

In Array Insert Nodes inside Node Programmaticaly

In fruits array I have two Object Fruits and vegetables. In Fruit object I have two nodes apple and mango. Under Apple node I have another two nodes Red apple and green apple.

Like this programatically I have to add another new node under vegetable.

$(function() {

var alternateData = [
{

text: 'Fruits',

nodes: [
{
text: 'apple',

nodes: [
{
text: 'Red apple'
},
{
text: 'green apple'
}
]
},
{
text: 'mango'
}
]
},
{
text: 'vegetables'

},


];

Answer
var alternateData = [{text: 'Fruits',
    nodes:[{text: 'apple',
          nodes: [{text: 'Red apple'},
                  {text: 'green apple'}]},
          {text: 'mango'}]},{text:'vegetables',nodes:[]}];

var newNode = {text:"veg1",nodes:[]};
alternateData[1].nodes.push(newNode);

As long as you define an empty node within the original array, you can push or pop any number of nodes into your "vegetables" element.