i have an array of objects i need to append the 0 index object to a div and 1 index object to another div. Using switch case returns from the initial case itself not executing the second index
let arr = [{id:1,name:'test'},{id:2,name:'sample'}]
arr.forEach((obj,index) => {
switch(index){
case 0:
$(".first p").text(obj.name)
case 1:
$(".second p").text(obj.name)
}
})
after executing the first case it returns not executing the case 1?
Thanks in advance
You need to add a break
statement to your cases, otherwise the execution will just "fall-through":
let arr = [{id:1, name:'test'}, {id:2, name:'sample'}];
arr.forEach((obj, index) => {
switch(index) {
case 0:
$(".first p").text(obj.name);
break;
case 1:
$(".second p").text(obj.name);
break;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="first">
<p/>
</div>
<div class="second">
<p/>
</div>