Michael Davis
Michael Davis

Reputation: 54

Iterating through an array of objects using $.each

Given the array of object below:

function person(first, last, RPI, o, t, u) {
    this.first = first;
    this.last = last;
    this.RPI =  RPI;
    this.o = o;
    this.t = t;
    this.u = u;
}

var MD = new person('Mike', 'D', 1234, '', '', '');
var AY = new person('Adam', 'Y', 5678, '', '', '');
var AH = new person('Adam', 'H', 1212, '', '', '');

var personArray = new Array(MD, AY, AH);

How would I iterate the RPI value from each object into this formula?

function selector(x){
//do something with x.RPI
}

I've tried:

$.each(personArray , selector (personArray[person].RPI){
selector(x)
});

But it doesn't work. What am I doing wrong with my each statement?

Upvotes: 0

Views: 28

Answers (2)

Shawn Northrop
Shawn Northrop

Reputation: 6016

The $.each callback needs to be a function Do something like the following:

var personArray = new Array (MW, MT, DR)
$.each(personArray, function(index, person){
   console.log(person.RPI);
}

Upvotes: 1

Jaromanda X
Jaromanda X

Reputation: 1

change your $.each to

$.each(personArray , selector);

and then

function selector(index, item){
    //do something with item.RPI
}

Upvotes: 0

Related Questions