jQuery Simple Example
HTML
<script src="//www.addressfinder.co.nz/assets/v2/widget.js"></script>
<label for="search_field">Address</label>
<input type='text' class='address_field' />
CSS
body {
padding: 5px;
}
label {
font-weight: bold;
}
input[type=text] {
width: 20em
}
p {
margin: 1em 0 0;
}
JavaScript
$(document).ready(function() {
var orig = [{
"id": "Test",
"parent": "#"
}, {
"id": "Test1",
"parent": "Test"
}, {
"id": "Test2",
"parent": "Test"
}, {
"id": "Test3",
"parent": "Test2"
}, {
"id": "Test4",
"parent": "Test1"
}, {
"id": "Test5",
"parent": "Test1"
}, {
"id": "Test6",
"parent": "Test4"
}];
function getChildren(id){
let result = [];
$.each(orig, function(i, d) {
if (d.parent === id) {
result.push(d);
result.push(...getChildren(d.id));
}
});
return result;
}
console.log(getChildren('Test'));
});