-1

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 ?

  • First thing you need to do is loop over student array ( you can use for, forEach methods), in each iteration you can check `checked` property on respective object by `. or []` notation – Code Maniac Jul 15 '22 at 03:58
  • Iterate over the array using for loop and check for condition or use filter method – gvmani Jul 15 '22 at 03:58
  • 1
    What have you tried? And, what problems did you run into. Certainly, you already know you can just iterate through the array and check each object for whatever condition you want, right? It's unclear where you got stuck and what exactly you need help with. – jfriend00 Jul 15 '22 at 03:58
  • Thanks for your valuable time. I got the answer – Lokesh Harode Jul 15 '22 at 04:11

1 Answers1

0

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]);
  }
} 
Max Webb
  • 152
  • 1
  • 14
  • Thanks, Max for your valuable time and your valuable contribution to my Question. After your solution Now I understand how to deal with these types of questions. – Lokesh Harode Jul 15 '22 at 04:15