I have this interface
export interface IFilaActividad {
[key: string]: any
}
And I create properties in it dynamically
var filaActividad: IFilaActividad = {}
this.columnsToDisplay.forEach(c => {
Object.defineProperty(filaActividad, c, {});
}
);
I see my properties and now I want to log them
for (let prop in filaActividad) {
console.log("Propiedades", prop);
}
But nothing is logged, never pass inside the for
Any idea, please?
Thanks
CodePudding user response:
By default, properties added using Object.defineProperty()
are not writable, not enumerable, and not configurable!
When you define a property this way, you can't overwrite it, delete or access via for...in
or Object.keys
by default! In order to make it possible to do so, you need to provide additional flags to the third parameter, the data descriptor object.
Insead of
Object.defineProperty(filaActividad, c, {});
... you should ...
Object.defineProperty(filaActividad, c, {
configurable: true,
writable: true,
enumerable: true,
value: undefined
});
CodePudding user response:
You don't see anything because all the values within your object are undefined
(based on the image posted).
If you want to see the keys
of your object, then just log the prop
:
for (let prop in filaActividad) {
console.log("key", prop);
console.log("the value of that prop: ", filaActividad[prop])
}
If you only interested in your object keys, you could also use Object.keys
:
const myKeys = Object.keys(filaActividad); // string[]
for (const key of myKeys) {
console.log("key : ", key);
}