Search code examples
jqueryhtmltreeviewdropdownbox

How to get access all selected elements data value in JosephSKh's DropdownTree


I want to access all data value of JosephSKh's DropdownTree like this

var arr4=[
            {title:"St Fatima",href:"#1",dataAttrs:[{title:"id",data:"value1"}]}
            ,
            {title:"Korba",href:"#2",dataAttrs:[{title:"id",data:"value4"}]}
            ,
            {title:"Roxi",href:"#3",dataAttrs:[{title:"id",data:"value7"}]}
        ];

From the above code, I want to access value1, value4, value7 if all are selected

Can anyone help me?


Solution

  • What you need is to loop through the array of Objects arr4 and fetch the deep nested property data. In order to do that, I recommend the following Javascript line:

    ES5 syntax:

    arr4.forEach(function(item) { console.log(item.dataAttrs[0].data); } );

    ES6 syntax:

    arr4.forEach(item => console.log(item.dataAttrs[0].data));

    Now, you should replace the console.log with the action you want. For example if you had an array to collect those values, you could edit the above line as:

    ES5 syntax:

    arr4.forEach(function(item) { babluArray.push(item.dataAttrs[0].data); } );

    ES6 syntax:

    arr4.forEach(item => babluArray.push(item.dataAttrs[0].data));

    Read further: forEach(), Arrow Functions