public Update() {
this.Data = this.Items;
console.log(this.Data);
for (let value of this.Data) {
console.log(value);
}
}
控制台
[Object, Object, Object]
Object
CandidateName:"B"
ControlId:0
CreatedBy:null
CreationDateTime:null
ExamId1:2000
ExamName:" Safety"
Id:1292353
在最后一个对象之后显示长度:3
当我要遍历这个对象时,抛出错误的长度是未定义的,请帮助我。
答案 0 :(得分:1)
对于迭代数组,应避免使用public class SimiliarPhotos extends AppCompatActivity implements IResult {
RecyclerView rv;
LinearLayoutManager llm;
ArrayList<Plant> plants = new ArrayList<Plant>();
SimiliarPlantsAdapter adapter;
VolleyService mVolleyService;
IResult mResultCallback = null;
final String GETREQUEST = "GETCALL";
//login url connection
final String URL = "http://10.0.2.2:3000/plants";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_similiar_photos);
rv = (RecyclerView)findViewById(R.id.rv);
llm = new LinearLayoutManager(this);
llm.setAutoMeasureEnabled(true);
rv.setLayoutManager(llm);
initializeAdapter();
initVolleyCallback();
mVolleyService = new VolleyService(mResultCallback,this);
mVolleyService.getDataVolley(GETREQUEST,URL);
}
@Override
public void notifySuccess(String requestType, JSONObject response) {
Log.d("resposta",response.toString());
}
@Override
public void notifySuccess(String requestType, JSONArray response) {
Log.d("resposta",response.toString());
}
@Override
public void notifyError(String requestType, VolleyError error) {
Log.d("resposta",error.toString());
}
void initVolleyCallback(){
mResultCallback = new IResult() {
@Override
public void notifySuccess(String requestType, JSONObject response) {
}
@Override
public void notifySuccess(String requestType, JSONArray response) {
Plant plant;
Log.d("ENTERED","ENTEREDHERE1");
// iterate over the JSONArray response
for (int i=0; i < response.length(); i++) {
try {
JSONObject object = response.getJSONObject(i); // get the individual object from JSONArray
int id = Integer.parseInt(object.getString("id")); // get the unique identifier from the object
String specie = object.getString("specie"); // get the name of the specie from the object
String description = object.getString("description"); // get the description of the object
ImageLoad(specie);
plant = new Plant(id,specie,description); // construct the object
Log.d("plant",String.valueOf(plant));
plants.add(plant); // add the object to the arraylist so it can be used on the cardLayout
} catch (JSONException e) {
Log.d("ENTERED",e.toString());
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
@Override
public void notifyError(String requestType, VolleyError error) {
Log.d("resposta",error.toString());
}
};
}
public void ImageLoad(String specie){
String urlFoto = "http://10.0.2.2:3000/images/" + specie + "/Thumbnail.jpg";
Picasso.with(this)
.load(urlFoto)
.into(imageView);
}
public void initializeAdapter(){
Log.d("plants",String.valueOf(plants.size()));
adapter = new SimiliarPlantsAdapter(plants);
rv.setAdapter(adapter);
}
}
或for in
语句。它有两个“缺点”:
1)订单不保证
2)如果可枚举,则列出/枚举继承的属性:定义属性时未指定false。
例如,如果您向原型添加属性,则此循环也将迭代该原型。
for of
您还应该看到您的财产已打印出来。
如果将循环更改为顺序for循环:
Array.prototype.test = "test";
var a = ['a', 'b'];
for (let i in a) {
console.log(a[i]);
}
for (let i of a) {
console.log(i);
}
或a:
for (let i = 0; i < this.Data.length; i++ value of this.Data) {
console.log(this.Data[i]);
}
您可能看不到自己的问题。
答案 1 :(得分:0)
如果我理解正确,this.Items
在某些情况下可能未定义,您无法迭代。
所以:
for (let value of (this.Data || [])) {
这可以防止不良价值
答案 2 :(得分:0)
如果要迭代对象,则必须使用Object.keys(your_obj)
。因为对象没有长度属性。您可以使用'Array'
仅迭代'String'
或for of
类型。您可以使用Object.keys(your_obj).length
进行顺序循环for(var i=0; i<Object.keys(your_obj).length; i++)
public Update() {
this.Data = this.Items;
console.log(this.Data);
for (let obj of this.Data) {
console.log(obj);
//iterate over object here
for(let property of Object.keys(obj)){
// write your logic for specific object
}
}
}
扩展到quirimmo的答案,顺序循环,使用此
this.Data.forEach(function(obj){
console.log(obj);
//iterate over object here
for(var i=0; i<Object.keys(obj).length; i++){
// write your logic for specific object
}
})