Navigating/Iterating in object as Interface 获取键值对
Navigating/Iterating in object as Interface to obtain key-value pair
export interface Worker {
salary?: number;
manager?: boolean;
name?: string;
hired?: Date;
}
const worker1: Worker = { salary: 5000, manager: true, name: "Bob", birth: new Date() };
我需要获取所有属性对作为键-值。
for (const [key, value] of worker1????) {
console.log("key: '" + key + "', value: " + value); // to do other needed operations
}
此日志问题的预期输出是:
key: 'salary', value: 5000
key: 'manager', value: true
key: 'name', value: Bob
key: 'birth', value: xxxxx
如何实现?
我想你只是想要 the Object.entries()
method?
for (const [key, value] of Object.entries(worker1)) {
console.log("key: '" + key + "', value: " + value);
}
export interface Worker {
salary?: number;
manager?: boolean;
name?: string;
hired?: Date;
}
const worker1: Worker = { salary: 5000, manager: true, name: "Bob", birth: new Date() };
我需要获取所有属性对作为键-值。
for (const [key, value] of worker1????) {
console.log("key: '" + key + "', value: " + value); // to do other needed operations
}
此日志问题的预期输出是:
key: 'salary', value: 5000
key: 'manager', value: true
key: 'name', value: Bob
key: 'birth', value: xxxxx
如何实现?
我想你只是想要 the Object.entries()
method?
for (const [key, value] of Object.entries(worker1)) {
console.log("key: '" + key + "', value: " + value);
}