编辑代码

/**
 * @file objToArray
 *
 * 将对象按照要求转为数组
 * 注意console示例运行结果
 */
type Obj = Record<string, string>;
interface FormatItem {
    key: string;
    op: string;
    value: string;
}

function objToArray(obj: Record<string, Obj>): FormatItem[] {
    // 补全此处代码
    let res: FormatItem[] = [];
    for (const e in obj) {
        console.log(e);
        const temp: FormatItem = {} as FormatItem;
        Object.keys(obj[e]).forEach(key => {
            temp.op = key;
            temp.value = obj[e][key];
        })
        temp.key = e;
        res.push(temp);
    }
    return res;
    //   throw new Error("功能待实现");
}

console.log(
    objToArray({
        key1: {
            op1: "value1",
        },
        key2: {
            op2: "value2",
        },
    })
);
// result示例
// [
//     {key: 'key1', op: 'op1', value: 'value1'},
//     {key: 'key2', op: 'op2', value: 'value2'}
// ]