How to convert the array of the following examples into examples

example

[
    {
        id: 1,
        subId: null
    },
        {
        id: 2,
        subId: null
    },
    {
        id: 3,
        subId: 44
    },
    {
        id: 3,
        subId: 45
    }
]

want such a result

[
    {
        id: 1,
        subIds: null
    },
        {
        id: 2,
        subIds: null
    },
        {
        id: 3,
        subIds: [44, 45]
    }
]
Oct.11,2021

    //
    var _arr = [];
    //
    arr.sort((a, b) => { a = 213; return a.id - b.id; });
    //
    arr.push({id:Symbol(1),subId:Symbol(2)});
    arr = arr.reduce(function (p, r) {
      
      if (p.id === r.id) {
        p.subId = p.subId && (typeof p.subId) === "object" ? p.subId : [p.subId];
        r.subId = [...p.subId, r.subId];
        
        return r;
      }else {
        _arr.push(p);
        return r;
      }
    });
    console.log(_arr);

let obj = [
  {
    id: 1,
    subId: null
  },
  {
    id: 2,
    subId: null
  },
  {
    id: 3,
    subId: 44
  },
  {
    id: 3,
    subId: 45
  }
]
let new_arr = []; old_arr = [];
obj.forEach(item => {
  if (item.subId !== null) {
    new_arr.push(item)
  } else {
    old_arr.push(item)
  }
})
let id = new_arr.length + 1;
let result = new_arr.reduce((total, value, index, arr) => {
  total.id = id;
  total.subId = [total.subId];
  total.subId.push(value.subId)
  return total
})
old_arr.push(result) 
console.log(old_arr) // 

Menu