Array.reduce使用场景

  1. a.b.c转成{a:{b:{c:1}}}

// strFormatJson('a.b', 1) return {a:{b:1}}
const strFormatJson = (str: string, val:string|number):any => {
return str.split('.').reverse().reduce((t:string|number, o:string) => ({[o] :t} as any), val);
};

应用reduce操作方式!!!
posted @ 2021-07-20 19:54  这月过后  阅读(92)  评论(0编辑  收藏  举报