使用lamda函数进行数据转换,及其map-reduce实现

  1. ["one", "two", "three"] =>  ['o', 'n', 'e', 't', 'w', 'o', 't', 'h', 'r', 'e', 'e']
a.map(it=>it.split("")).reduce((a,b)=>[...a, ...b])
a.flatMap(it=>it.split(""))
a.map(it=>it.split("")).flat()
  1. [{ classId: 1, className: "一班", stuId: 1001, stuName: "张三" },{ classId: 1, className: "一班", stuId: 1002, stuName: "李四" }]=> [{"classId":1,"className":"一班","stuList":[{"stuId":1001,"stuName":"张三"},{"stuId":1002,"stuName":"李四"}]}]
let a = [{ classId: 1, className: "一班", stuId: 1001, stuName: "张三" },
{ classId: 1, className: "一班", stuId: 1002, stuName: "李四" }]

let res = a.reduce((i, { classId, className, stuId, stuName }) => {
    if (!i[classId]) i[classId] = { classId, className, stuList: [] }
    i[classId].stuList.push({ stuId, stuName })
    return i
}, {})

console.log(JSON.stringify(Object.values(res)))
posted @ 2025-04-13 21:51  豆苗稀  阅读(16)  评论(0)    收藏  举报