我从firebase中撤下并获得了一个包含3个对象的数组的有效负载,其中包含对象。
请考虑以下屏幕截图:
当我在console.log中找到我的月份数组时会发生这种情况。
如何迭代这个以从对象内部注销我想要的任何信息?
例如我想要在月内和p1,p2内部。
我试过做
this.state.months.map()
然后记录month.month
,但返回undefined。
任何想法?
答案 0 :(得分:0)
如何在for-in
循环中对数组使用for
循环?
for (var i = 0, l = this.state.months.length; i < l; i++) {
var obj = this.state.months[i];
for (var key in obj) {
//check if obj owns the property and not some prototype
if (obj.hasOwnProperty(key)) {
console.log(obj[key].p1); //what do we do with p1?
console.log(obj[key].p2); //what do we do with p2?
}
}
}
答案 1 :(得分:0)
尝试这种方式
jList = this.state.months
jList.map(jList=>(jList.month+ ", "+ jList.p1 + ", " + jList.p2))
OUTPUT - 将是一个数组
["some-Monthname, p1-someval, p2-someval", ... ]
答案 2 :(得分:0)
根据您提供的屏幕截图,您有一个包含月份对象的对象数组。您可以循环遍历该数组,然后将它们映射到仅包含月份及其信息的数组中。这将使处理这几个月更容易管理。
var months = this.state.months.forEach( function( item ) {
return { month: item.month, p1: item.p1, p2: item.p2 };
});
然后您可以访问月份及其数据。
console.log( 'Month is ', months[0].month, '. p1 object data is ', months[0].p1 '. and p2 object data is ', months[0].p2);