I want get all values the my Object Month ->
public months: Months = {
jan: false,
feb: false,
etc...
}
First my object is false but then my user changed a true, then I need know that month is true.
I don't want do 12 if ...
if (months.jan) { ... }
I am trying with
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 );
}
or with ->
for (prop of this.months) { //Now I get error because this.month isn't array.
console.log('prop', prop );
}
thanks.
Edit ->
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;
}
}
}
Use the in
operator to loop over Objects
instead of 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] ); }
Use for in loop.
for (key in this.months) {
console.log('prop', this.months[key] );
}
use 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]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.