I have a UI where I have Some check boxes I am trying to convert it into an array of objects. An objects has 3 keys Counter
,Name
and IsActive
all this are objects and will be inside the array
When checkbox is on isActive
should be Y, I am here to get some good approach according to the task I have already done
What I have done
var Data = {
"Counter A": ["CounterA1.jpg", "CounterA2.jpg"],
"Counter B": ["CounterB1.jpg"],
"Counter C": ["CounterC1.jpg"]
}
var counter = {};
var name = {};
var flag = {};
for (var key in Data) {
var newCard = $(`<div>
<div class="card-header"></div>
<ul class="list-group list-group-flush">
</ul>
</div>`);
var ul_innerhtml = "";
$(".card-header", newCard).text(key);
Data[key].forEach(d => {
ul_innerhtml += '<li class="list-group-item">' + d + '<label class="switch "><input name="type" type="checkbox" class="success" value="' + d + '"><span class="slider round"> </span></label></li>'
counter = {
"Counter": key
} // geting name but it is only giving last counter i.e C
name = {
"Name": d
} // same fpor this
});
$(".list-group", newCard).append(ul_innerhtml);
$(".card").append(newCard.html());
}
$("#btn").click(function() {
console.log(counter) // printing counter C only
})
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
float: right;
}
/* Hide default HTML checkbox */
.switch input {
display: none;
}
/* The slider */
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input.success:checked+.slider {
background-color: #8bc34a;
}
input:checked+.slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<div class="container">
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
<div class="card" style="margin: 10px 0">
</div>
</div>
<button id="btn" class="btn btn-default commonButton" type="submit">
<i class="fa fa-search"></i> Go
</button>
</div>
On click of go I am printing the data but it is giving only one data
i want to create object like
[ { "Counter": "Counter A", "Name": "CountA1.jpg", "IsActive":"Y" },
{ "Counter": "Counter A", "Name": "CountA2.jpg", "IsActive":"N" },
{ "Counter": "Counter B", "Name": "CountB1.jpg", "IsActive":"Y" },
{ "Counter": "Counter C", "Name": "CountC1.jpg", "IsActive":"Y" }]
because I need this structure only for my future work