OneCompiler

working with object datatype

149

// nested object

var obj={
13690073:
{
name: 'Taiwo yusuf',
sch: 'Moshood Abiola Poly',
dept: 'Computer Science',
course: ['COM101', 'MTH101', 'COM102']
},

13690024:
{
name: 'Ogunoye Tobi',
sch: 'Moshood Abiola Poly',
dept: 'Computer Science',
course: ['COM101', 'MTH101', 'COM102']
},

13690002:
{
name: 'Oyelekan Tobi',
sch: 'Moshood Abiola Poly',
dept: 'Computer Science',
course: ['COM101', 'MTH101', 'COM102']
},

13720098:
{
name: 'Abolarinwa Tope',
sch: 'Moshood Abiola Poly',
dept: 'Computer Engineer',
course: ['COM101', 'MTH101', 'COM102']
}
};

function myfun(id,n,s,d,c){

let valid = obj[id];

for(let user in obj){
  if(id === user){
    
    if(obj[id].course.length <= 1){
      
      obj[id].course.push(c);
    }
    
    if(obj[id]['dept'] === ''){
      obj[id]['dept'] = d;
    }
    
    let ns = (obj[id]['sch']).toLowerCase();
    s.toLowerCase();
    if(ns !== s){
      obj[id]['sch'] = 'Moshood Abiola Poly';
    }
    
  }
}

return obj[id];

}

let result = myfun('13690002','','','','STA101');
console.log(result);