I have a string, and I have to insert "div" into the string by using start/end values from an array.
var str = "this is a simple string";
var places = [{
"start": 0,
"end": 3
},
{
"start": 9,
"end": 15
}];
function insert(places, str) {
// I tryed different approaches like this:
str = str.forEach(function(item, index) {
var start = item.start;
var end = item.end;
var newStr = '<div>'+str[start] + '</div>' + str.slice(end);
str = newStr;
});
$('.h').html(str);
// I need to get output str as:
// "<div>this</div> is a <div>simple</div> string"
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="h"></div>
How can I do it using splice or foreach functions?