javascript – How to find sum of a specific property in nested JS object

const object = {
  val: 10,
  child: (
    {
      val: 20,
      child: (
        {
          val: 25,
          child: ()
        },
        {
          val: 28,
          child: ()
        }
      )
    },
    {
      val: 30,
      child: ()
    }
  )
};

function sum() {
 let k = object.val;
 if(object.child.length>0){
 object.child.map(item => {
 
 k += item.val;
 if(item.child.length>0){
 item.child.map(item => k += item.val)
 }
 })
 }
 
 return k
}

const result = sum(object);
console.log(result);