const student= [
{
student_id: 0,
name: "Rhoda Salas",
checked:"checked"
},
{
student_id: 1,
name: "Jewell Avery"
},
{
student_id: 2,
name: "Susanne Harris",
checked:"checked"
}
]
console.log(student)
As we know that, I have student variable which contain array of object.
Now, I want to check (find) or count which object has contained checked key.
So How can I find and how many ways ?
Welcome to stack overflow Lokesh! For me, the simplest solution that I would do is to iterate over the array, and check for where the key checked is equal to checked. However, someone else might have a better solution.
for (var i = 0; i < student.length; i++) {
if (student[i].checked === 'checked') {
console.log(student[i]);
}
}
If you want to count that, you could add a simple counter variable to the code to, like so.
var counter = 0;
for (var i = 0; i < student.length; i++) {
if (student[i].checked === 'checked') {
counter++
console.log(student[i]);
}
}