[英]get value the 1 Object in Angular5
我想在“对象月”中获取所有值->
public months: Months = {
jan: false,
feb: false,
etc...
}
首先,我的对象为false,但后来用户更改为true,然后我需要知道月份为true。
我不想做12如果...
if (months.jan) { ... }
我正在尝试
let responseProps = Object.keys(this.months);
console.log('responseProps ', responseProps ); //I get 0º->jan, 1->feb..
// but I don't get "true or false"
for (prop of responseProps) {
console.log('prop', prop );
}
或->
for (prop of this.months) { //Now I get error because this.month isn't array.
console.log('prop', prop );
}
谢谢。
编辑->
public months: Months = {
jan: false, -> index 0
feb: false, -> index 1
etc...
}
searchMonth(year: string, selectMonth: number) {
if (selectMonth === undefined) {
let obKeys = Object.keys(this.months), prop: string, month: number;
/*Get first Month*/
for (prop of obKeys) {
if (this.months[prop]) {
month = prop; // HERE I NEED the number of month
break;
}
}
}
使用in
以上操作循环Objects
,而不是of
。
var months = { jan: false, feb: true, } for (var eachMonth in months) { // in operator will also return true for props in prototype chain, hence the below check if (months.hasOwnProperty(eachMonth)) { console.log(eachMonth, months[eachMonth]); } } // If you want to use Object.keys() var obKeys = Object.keys(months); // this will not give props from prototype, so no further check for (prop of obKeys) { console.log('prop: ', prop, ", month index: ", obKeys.indexOf(prop), "value: ", months[prop] ); }
用于循环。
for (key in this.months) {
console.log('prop', this.months[key] );
}
使用this.months[prop]
let responseProps = Object.keys(this.months);
console.log('responseProps ', responseProps);
// but I don't get "true or false"
for (prop in this.months) {
console.log('prop', this.months[prop]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.