我正在尝试迭代一个对象数组而不是它只返回一个项目代码:
setAll(){
var result =this.cart;
for (var key in result) {
var obj = result[key];
}
return obj.price;
}
和测试数据
[ { "id": 5, "price": 3200, "quantity": 8, "name": "juice" }, { "id": 6,
"price": 300, "quantity": 6, "name": "rice" }, { "id": 8, "price": "100",
"quantity": 1, "name": "water" }, { "id": 7, "price": "4500", "quantity":
1, "name": "meat" } ]
答案 0 :(得分:1)
您正在遍历数组并仅返回最后一个对象。这段代码:
private static Row avroToRowConverter(GenericRecord avroRecord) {
if (null == avroRecord) {
return null;
}
//GenericData
Object[] objectArray = new Object[avroRecord.getSchema().getFields().size()];
StructType structType = (StructType) SchemaConverters.toSqlType(avroRecord.getSchema()).dataType();
for (Schema.Field field : avroRecord.getSchema().getFields()) {
if(field.schema().getType().toString().equalsIgnoreCase("STRING") || field.schema().getType().toString().equalsIgnoreCase("ENUM")){
objectArray[field.pos()] = ""+avroRecord.get(field.pos());
}else {
objectArray[field.pos()] = avroRecord.get(field.pos());
}
}
return new GenericRowWithSchema(objectArray, structType);
}
除了将for (var key in result) {
var obj = result[key];
}
设置为列表中的最后一项外,别无其他任何操作。
答案 1 :(得分:0)
现在df['date_of_reservation'] = pd.to_datetime(df['date_of_reservation'])
df_date = pd.DataFrame(df.groupby(pd.Grouper(key='date_of_reservation', freq="d"))['number_of_bookings'].mean())
df_date=df_date.reset_index()
df2=pd.merge(df,df_date[['date_of_reservation']], on='date_of_reservation', how='right').sort_values('date_of_reservation')
df2.loc[df2['number_of_bookings'].isnull(), 'number_of_bookings'] = 0
df2.fillna(method='ffill', inplace=True)
将被设置为从上一次迭代到循环的值,并且您将仅返回该对象的价格。
答案 2 :(得分:0)
答案 3 :(得分:0)
这对我有用:
setAll(){
var result =this.cart;
var res=Object.keys(result).map(function(key){
return parseInt(result[key].price);
});
return res;
},