Javascript React-for循环只返回索引,不返回整个对象
我有一个组件,其状态为以下对象(“项”):Javascript React-for循环只返回索引,不返回整个对象,javascript,reactjs,Javascript,Reactjs,我有一个组件,其状态为以下对象(“项”): items: count: 2 next: null previous: null results: Array[2] 0: {…} id: 1 price: "100.00" show_in_shop: true tax_percentage: "5.0000000" title: 'Object title' type: "assessment
items:
count: 2
next: null
previous: null
results: Array[2]
0: {…}
id: 1
price: "100.00"
show_in_shop: true
tax_percentage: "5.0000000"
title: 'Object title'
type: "assessment"
1: {…}
id: 2
price: "1000.00"
show_in_shop: true
tax_percentage:"8.0000000"
title: "Title of this object"
type: "class"
我正在创建一个在this.state.items.results上循环的筛选器,并检查Item对象的值是否在搜索查询中
但是,在运行此代码时,我遇到了以下问题:
for (let item in this.state.items.results) {
console.log(item);
打印到控制台的是
0
1
而不是:
0: {…}
id: 1
price: "100.00"
show_in_shop: true
tax_percentage: "5.0000000"
title: 'Object title'
type: "assessment"
1: {…}
id: 2
price: "1000.00"
show_in_shop: true
tax_percentage:"8.0000000"
title: "Title of this object"
type: "class"
但是为什么呢?当我检查react chrome插件中的状态时,我看到“item.results”中填充了数据,并且包含的数据比打印到控制台的数据还要多。有人知道为什么循环只在索引上循环而不返回对象吗 javascript
for。。实际上,in
只返回索引
使用:
javascript
for。。实际上,in
只返回索引
使用:
将仅提供索引,如果您希望每个对象都使用
像这样:
var-arr=[{a:1},{a:2}];
对于(让el代表arr){
控制台日志(el);
}
将仅提供索引,如果您需要每个对象的索引,请使用
像这样:
var-arr=[{a:1},{a:2}];
对于(让el代表arr){
控制台日志(el);
}
将用于
for (let item of this.state.items.results) {
console.log(item);
}
将用于
for (let item of this.state.items.results) {
console.log(item);
}
for…in将返回集合的索引,而不是集合的项。 例如:
for…in将返回集合的索引,而不是集合的项。 例如:
for (let item of this.state.items.results) {
console.log(item);
}
var array = [10,20,30];
for(var item in array){
console.log(array[item]);
}