ts 枚举转键值数组再转对象

interface Data { name?: string; };
const data: Data[] = [{ name: '1' }, { name: '2' }];
const Name = { '1': '小明', '2': '小强' };
const downloadData = (data: Data[]) => {
      return data.map(res => {
        const text = Object.fromEntries(Object.entries(Name).filter(([value, label]) => res.name === value))['' + res.name];
        text && (res.name = text)
        return res;
      })
};
console.log(downloadData(data));//[{name:'小明'},{name:'小强'}]

你可能感兴趣的:(ts 枚举转键值数组再转对象)