Every same child type should wrap in div. The problem is I am getting single array which contains all the children. So I am running loop over it and tried to create new div and close it when children type changed but it is not well written.
var items = [{
"type": "child1",
"name": "1 "
}, {
"type": "child1",
"name": "1 "
}, {
"type": "child1",
"name": "1 "
}, {
"type": "child2",
"name": "2 "
}, {
"type": "child2",
"name": "2"
}, {
"type": "child3",
"name": "3 "
}, {
"type": "child3",
"name": "3 "
}, {
"type": "child3",
"name": "3"
}]
var child = "";
var html = ''
items.forEach(function(item) {
if (child !== item.type) {
html += '<div>'
}
html += '<div>' + item.name + ' </div>';
if (child !== item.type && child) {
html += '</div>'
}
child = item.type;
})
document.getElementById('html').innerHTML = html;
div { border:1px solid black }
<div id="html"></div>