0

Im pretty new to javascript and would love some help on my problem here. So what I am trying to do is to extract the value of "group" from my object array using the id value. The object array is in the following format: y = [{"id":id,"group":group, "sid":sid}]. Have looped through the array as shown below. How do I extract the group value? Thanks!

y = [{id:123, group:2, sid:32}];
id = 123

      for (var i = 0; i < y.length; i++) {
        if (y[i].id == id) {
        //Get group value for id "123" here

        }
      }
user3702643
  • 1,465
  • 5
  • 21
  • 48

1 Answers1

0

Just use the object that matches your condition (y[i]) and refer to the property group of it :

if (y[i].id == id) {
  var group = y[i].group;
}

group is a property that is accessible as any property defined in the objects that you are manipulating (as for example : id or sid)

davidxxx
  • 125,838
  • 23
  • 214
  • 215