-1

I have an array of objects, such as this:

var elements = [
{
    LabCode: 'VA',
    Zscore: 0.5,
    Standard: 'std1' 
},

{
    LabCode: 'RE',
    Zscore: 0.53,
    Standard: 'std1' 
},
{
    LabCode: 'VO',
    Zscore: 1.5,
    Standard: 'std1' 
},
{
    LabCode: 'VA',
    Zscore: 3.4,
    Standard: 'std2' 
},
{
    LabCode: 'RE',
    Zscore: 2.45,
    Standard: 'std2' 
},
{
    LabCode: 'VO',
    Zscore: 1.67,
    Standard: 'std2' 
}
]

What I need is this:

var result = [
{
Standard: 'std1',
VA: 0.5,
RE: 0.53,
VO: 1.5
},

{
Standard: 'std2',
VA: 3.4,
RE: 2.45,
VO: 1.67
}
]

Labcodes are dynamic so I need to be able to dynamically creates the resulted object. I can have many standards and they are also dynamic.

I am using lodash.

Please note that the property name needs to reflect the fact that labcodes are dynamic as well. This was not addressed in the answer you notes as duplicate.

Larry Shatzer
  • 3,579
  • 8
  • 29
  • 36
sarsnake
  • 26,667
  • 58
  • 180
  • 286

2 Answers2

1

You can do it simply with reduce() like this

var elements = [
                {LabCode: 'VA',Zscore: 0.5, Standard: 'std1' },
                {LabCode: 'RE',Zscore: 0.53,Standard: 'std1' },
                {LabCode: 'VO',Zscore: 1.5, Standard: 'std1' },
                {LabCode: 'VA',Zscore: 3.4, Standard: 'std2' },
                {LabCode: 'RE',Zscore: 2.45,Standard: 'std2' },
                {LabCode: 'VO',Zscore: 1.67,Standard: 'std2' }
               ]

let final = Object.values(elements.reduce((op,cur)=>{
  if( op[cur['Standard']] ) {
    op[cur['Standard']][cur['LabCode']] = cur['Zscore']
  } else {
    op[cur['Standard']] = {
     'Standard' : cur['Standard'],
     [cur['LabCode']]: cur['Zscore']
    }
  }
  return op;
}, {} ))

console.log(final);

Well in case of IE you can use this.

var elements = [
                    {LabCode: 'VA',Zscore: 0.5, Standard: 'std1' },
                    {LabCode: 'RE',Zscore: 0.53,Standard: 'std1' },
                    {LabCode: 'VO',Zscore: 1.5, Standard: 'std1' },
                    {LabCode: 'VA',Zscore: 3.4, Standard: 'std2' },
                    {LabCode: 'RE',Zscore: 2.45,Standard: 'std2' },
                    {LabCode: 'VO',Zscore: 1.67,Standard: 'std2' }
                   ]

    let final = elements.reduce((op,cur)=>{
      if( op[cur['Standard']] ) {
        op[cur['Standard']][cur['LabCode']] = cur['Zscore']
      } else {
        op[cur['Standard']] = {
         'Standard' : cur['Standard'],
         [cur['LabCode']]: cur['Zscore']
        }
      }
      return op;
    }, {} )
let finalop = [];
for(let key in final){
  finalop.push(final[key])
}
    console.log(finalop);
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • 2
    I don't think this answer makes use of Lodash. –  Dec 14 '18 at 17:52
  • @sarsnake this is using just normal js. you don't need any loadash for this solution. check the demo – Code Maniac Dec 14 '18 at 17:52
  • @sarsnake you can read up here about object keys alternate in loadash https://stackoverflow.com/questions/35566472/object-keys-equivalent-lodash-method – Code Maniac Dec 14 '18 at 18:05
  • Sorry. Object.values is not supported in IE https://stackoverflow.com/questions/42830257/alternative-version-for-object-values – sarsnake Dec 14 '18 at 18:26
  • 1
    @sarsnake you should mention such things in question itself. anyways added a version for IE also. – Code Maniac Dec 14 '18 at 18:38
  • @code maniac Yeah sorry I should have - I just assume that IE needs to be supported since so many people use it. By the way arrow syntax is not supported in IE either..... – sarsnake Dec 14 '18 at 18:45
  • @sarsnake you can trans-pile it using https://babeljs.io/ – Code Maniac Dec 14 '18 at 18:48
0

You can get this done via one Array.reduce and some ES6 (without lodash):

var data = [{ LabCode: 'VA', Zscore: 0.5, Standard: 'std1' }, { LabCode: 'RE', Zscore: 0.53, Standard: 'std1' }, { LabCode: 'VO', Zscore: 1.5, Standard: 'std1' }, { LabCode: 'VA', Zscore: 3.4, Standard: 'std2' }, { LabCode: 'RE', Zscore: 2.45, Standard: 'std2' }, { LabCode: 'VO', Zscore: 1.67, Standard: 'std2' } ] 

const result = data.reduce((r, {LabCode, Zscore, Standard}, i, a) => {
  r[Standard] = r[Standard] || {}
  r[Standard][LabCode] = Zscore
  return i == a.length-1 ? Object.keys(r).map(k => ({Standard: k, ...r[k]})) : r
}, {})

console.log(result)
Akrion
  • 18,117
  • 1
  • 34
  • 54