使用$ .each迭代一系列对象

时间:2017-01-11 02:46:48

标签: javascript jquery

给出下面的对象数组:

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);

如何将每个对象的RPI值迭代到此公式中?

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

我试过了:

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

但它不起作用。我的每个陈述我做错了什么?

2 个答案:

答案 0 :(得分:1)

$.each回调需要是一个函数 做类似以下的事情:

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

答案 1 :(得分:0)

将$ .each更改为

$.each(personArray , selector);

然后

function selector(index, item){
    //do something with item.RPI
}
相关问题