Home > Software engineering >  How to convert an array into multiple objects with keys?
How to convert an array into multiple objects with keys?

Time:10-28

I'm unable to optimize the function to convert an array into multiple objects with keys for my desired output.

const students = [
  ['Name01', 'Sub01', 'y', 'Sub02', 'y', 'Sub03', 'y', 'Sub04', 'y', 'OverAll', 'y', 'SecA'],
  ['Name02', 'Sub01', 'n', 'Sub02', 'y', 'Sub03', 'y', 'Sub04', 'y', 'OverAll', 'n', 'SecA'],
  ['Name03', 'Sub01', 'y', 'Sub02', 'y', 'Sub03', 'n', 'Sub04', 'y', 'OverAll', 'n', 'SecB'],
  ['Name04', 'Sub01', 'y', 'Sub02', 'y', 'Sub03', 'y', 'Sub04', 'y', 'OverAll', 'y', 'SecB'],
  ['Name05', 'Sub01', 'y', 'Sub02', 'y', 'Sub03', 'y', 'Sub04', 'n', 'OverAll', 'n', 'SecB']];

const newArr = students.reduce((a, [A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, Sec]) => {
  a[A1] = { Name: A1, Sub01: A3, Sub02: A5, Sub03: A3, Sub04: A7, Sub05: A9, OverAll: A11, Sec }
  return a;
}, {});

console.log(newArr)

And following is the output result for above code:

{
  Name01: {
    Name: 'Name01', Sub01: 'y', Sub02: 'y', Sub03: 'y', Sub04: 'y', Sub05: 'y', OverAll: 'y', Sec: 'SecA'
  },
  Name02: {
    Name: 'Name02', Sub01: 'n', Sub02: 'y', Sub03: 'n', Sub04: 'y', Sub05: 'y', OverAll: 'n', Sec: 'SecA'
  },
  Name03: {
    Name: 'Name03', Sub01: 'y', Sub02: 'y', Sub03: 'y', Sub04: 'n', Sub05: 'y', OverAll: 'n', Sec: 'SecB'
  },
  Name04: {
    Name: 'Name04', Sub01: 'y', Sub02: 'y', Sub03: 'y', Sub04: 'y', Sub05: 'y', OverAll: 'y', Sec: 'SecB'
  },
  Name05: {
    Name: 'Name05', Sub01: 'y', Sub02: 'y', Sub03: 'y', Sub04: 'y', Sub05: 'n', OverAll: 'n', Sec: 'SecB'
  }
}

I'm trying to optimize the code for desired output result as follows:

{
  Pass: {
    SecA: {
      Name01: { Name: 'Name01', Sub01: 'y', Sub02: 'y', Sub03: 'y', Sub04: 'y', Sub05: 'y' }
    },
    SecB: {
      Name04: { Name: 'Name04', Sub01: 'y', Sub02: 'y', Sub03: 'y', Sub04: 'y', Sub05: 'y' }
    }
  },
  Fail: {
    SecA: {
      Name02: { Name: 'Name02', Sub01: 'n', Sub02: 'y', Sub03: 'n', Sub04: 'y', Sub05: 'y' }
    },
    SecB: {
      Name03: { Name: 'Name03', Sub01: 'y', Sub02: 'y', Sub03: 'y', Sub04: 'n', Sub05: 'y' },
      Name05: { Name: 'Name05', Sub01: 'y', Sub02: 'y', Sub03: 'y', Sub04: 'y', Sub05: 'n' }
    }
  }
}

CodePudding user response:

const students = [
  ['Name01', 'Sub01', 'y', 'Sub02', 'y', 'Sub03', 'y', 'Sub04', 'y', 'OverAll', 'y', 'SecA'],
  ['Name02', 'Sub01', 'n', 'Sub02', 'y', 'Sub03', 'y', 'Sub04', 'y', 'OverAll', 'n', 'SecA'],
  ['Name03', 'Sub01', 'y', 'Sub02', 'y', 'Sub03', 'n', 'Sub04', 'y', 'OverAll', 'n', 'SecB'],
  ['Name04', 'Sub01', 'y', 'Sub02', 'y', 'Sub03', 'y', 'Sub04', 'y', 'OverAll', 'y', 'SecB'],
  ['Name05', 'Sub01', 'y', 'Sub02', 'y', 'Sub03', 'y', 'Sub04', 'n', 'OverAll', 'n', 'SecB']
];

const newArr = students.reduce((a, [A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, Sec]) => {
  (a[A11] = a[A11] || []).push(A1 = {
    SUB01: A3,
    SUB02: A5,
    SUB03: A7,
    SUB04: A9,
  });
  return a;
}, {});
console.log(newArr)

CodePudding user response:

    const students = [
      ['Name01', 'Sub01', 'y', 'Sub02', 'y', 'Sub03', 'y', 'Sub04', 'y', 'OverAll', 'y', 'SecA'],
      ['Name02', 'Sub01', 'n', 'Sub02', 'y', 'Sub03', 'y', 'Sub04', 'y', 'OverAll', 'n', 'SecA'],
      ['Name03', 'Sub01', 'y', 'Sub02', 'y', 'Sub03', 'n', 'Sub04', 'y', 'OverAll', 'n', 'SecB'],
      ['Name04', 'Sub01', 'y', 'Sub02', 'y', 'Sub03', 'y', 'Sub04', 'y', 'OverAll', 'y', 'SecB'],
      ['Name05', 'Sub01', 'y', 'Sub02', 'y', 'Sub03', 'y', 'Sub04', 'n', 'OverAll', 'n', 'SecB']
      
      ];
    function structurData(){
        let finalObj={
            Pass:{
                
            },
            Fail:{
                
            }
        }
        students.forEach(function(innerArray,index){
            let name=innerArray[0];
            let section=innerArray[innerArray.length-1];
            let statusflag="Pass";
            let innerObj={};
            for(var indx=1;indx<innerArray.length-1;indx=indx 2){
                let subject=innerArray[indx];
                let flag=innerArray[indx 1];
                
                if(statusflag!="Fail"){
                    statusflag=innerArray[indx 1]=="y"?"Pass":"Fail";
                }
                innerObj[subject]=flag;
                
            }
            innerObj["Name"]=name;
            if(!(section in finalObj[statusflag])){
                finalObj[statusflag][section]={};
            }
            finalObj[statusflag][section][name]=innerObj;
        
            
        });
        console.log(finalObj);
        
    }
    structurData();

  • Related