1/** 2 * @file objToArray 3 * 4 * 将对象按照要求转为数组 5 * 注意console示例运行结果 6 */ 7type Obj = Record<string, string>; 8interface FormatItem { 9 key: string; 10 op: string; 11 value: string; 12} 13 14function objToArray(obj: Record<string, Obj>): FormatItem[] { 15 // 补全此处代码 16 throw new Error("功能待实现"); 17} 18 19console.log( 20 objToArray({ 21 key1: { 22 op1: "value1", 23 }, 24 key2: { 25 op2: "value2", 26 }, 27 }) 28); 29// result示例 30// [ 31// {key: 'key1', op: 'op1', value: 'value1'}, 32// {key: 'key2', op: 'op2', value: 'value2'} 33// ] 34 35export default {};
参考答案:
参考答案:
1/** 2 * @file objToArray 3 * 4 * 将对象按照要求转为数组 5 * 注意console示例运行结果 6 */ 7type Obj = Record<string, string>; 8interface FormatItem { 9 key: string; 10 op: string; 11 value: string; 12} 13 14function objToArray(obj: Record<string, Obj>): FormatItem[] { 15 return Object.keys(obj).reduce((value: Array<FormatItem>, key: string) => { 16 var op: string = Object.keys(obj[key])[0]; 17 value.push({ key: key, op: op, value: obj[key][op] }); 18 return value; 19 }, []); 20} 21 22console.log( 23 objToArray({ 24 key1: { 25 op1: "value1", 26 }, 27 key2: { 28 op2: "value2", 29 }, 30 }) 31); 32// result示例 33// [ 34// {key: 'key1', op: 'op1', value: 'value1'}, 35// {key: 'key2', op: 'op2', value: 'value2'} 36// ] 37 38export default {};
最近更新时间:2024-08-10