简体   繁体   English

使用$ .each遍历对象数组

[英]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? 如何将每个对象的RPI值迭代到此公式中?

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? 我的每句话都错在哪里?

The $.each callback needs to be a function Do something like the following: $.each回调必须是一个函数,请执行以下操作:

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

change your $.each to 将您的$ .each更改为

$.each(personArray , selector);

and then 接着

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM