AnswerBun.com

Using lodash to modify object

Stack Overflow Asked by Prova12 on January 1, 2022

I am trying to transform an array of objects (aka initial objects) into an array with the objects re arranged.
Basically I want to create an array which contains a new object. This new object has at level zero, some property of the initial objects and a nested array that contains other value from the same initial objects
To do so, I am using lodash but I am missing something.

My initial array

  const myArray = {
    ages: [
      {
        maxAge: 32,
        minAge: 20,
        name: "20-32",
        percent: 20,
        quotaGroupName: "Age"
      },
      {
        maxAge: 40,
        minAge: 33,
        name: "33-40",
        percent: 50,
        quotaGroupName: "Age"
      }
    ],
    genders: [
      {
        gender: 1
        name: "Male"
        percent: 40
        quotaGroupName: "Gender"
      },
      {
        gender: 2
        name: "Female"
        percent: 60
        quotaGroupName: "Gender"
      },
    ]
    }

I would like to obtain something like:

  groups: [
    {
      groupType: "Age", // quotaGroupName
      quotas: [
        {
          name: "20-32",
          percent: 20,
          targetGroup: {
            minAge: 20,
            maxAge: 32,
          }
        },
        {
          name: "33-40",
          percent: 50,
          targetGroup: {
            minAge: 40,
            maxAge: 30,
          }
        }
      ]
    },
    {
      groupType: "Gender", // quotaGroupName
      quotas: [
        {
          name: "Male",
          percent: 20,
          targetGroup: {
            variableIds: [1], // gender
          }
        },
        {
          name: "Female",
          percent: 50,
          targetGroup: {
            variableIds: [2], // gender
          }
        }
      ]
    }
  ]

I am trying with:

  _.chain(data)
    .groupBy("groupType")
    .map((value, key) => (something))
    .value()

but I am really stuck and not sure how to proceede

2 Answers

You can do it using lodash:

_.chain(myArray)
  .map((items, key) => ({
    groupType: _.get(_.first(items), 'quotaGroupName'),
    quotas: _.map(items, item => {
      return _.assign(_.pick(item, ['name', 'percent']), {
        targetGroup: key === 'genders' ? {variableIds: [item.gender]} : 
        _.omit(item, ['name', 'percent'])
      })
    })
  }))

Answered by Victoria Iashchuk on January 1, 2022

Remember that with ES6, most times you actually don't need Lodash! I wrote a function getTargetGroup that you can make as complicated as you want to suit your use case, but otherwise, this should suit your needs.

const myData = {
    ages: [{
        maxAge: 32,
        minAge: 20,
        name: "20-32",
        percent: 20,
        quotaGroupName: "Age",
    },
        {
            maxAge: 40,
            minAge: 33,
            name: "33-40",
            percent: 50,
            quotaGroupName: "Age",
        },
    ],
    genders: [{
        gender: 1,
        name: "Male",
        percent: 40,
        quotaGroupName: "Gender",
    },
        {
            gender: 2,
            name: "Female",
            percent: 60,
            quotaGroupName: "Gender",
        },
    ],
};

const getTargetGroup = (item) => {
  if (item.minAge !== undefined && item.maxAge !== undefined) {
    return { minAge: item.minAge, maxAge: item.maxAge };
  }
  
  return {variableIds: [item.gender]};
};

const result = {
    groups: Object
        .values(myData)
        // First `items` === `myData.ages`
        .map((items) => ({
            groupType: items[0].quotaGroupName,
            // First `item` === `myData.ages[0]`
            quotas: items.map((item) => ({
                name: item.name,
                percent: item.percent,
                targetGroup: getTargetGroup(item),
            })),
        })),
};

console.log(result);
document.getElementById('actual').innerHTML = JSON.stringify(result);
<code id="expected">
groups: [
    {
      groupType: "Age",
      quotas: [
        {
          name: "20-32",
          percent: 20,
          targetGroup: {
            minAge: 20,
            maxAge: 32,
          }
        },
        {
          name: "33-40",
          percent: 50,
          targetGroup: {
            minAge: 40,
            maxAge: 30,
          }
        }
      ]
    },
    {
      groupType: "Gender",
      quotas: [
        {
          name: "Male",
          percent: 20,
          targetGroup: {
            variableIds: [1],
          }
        },
        {
          name: "Female",
          percent: 50,
          targetGroup: {
            variableIds: [2],
          }
        }
      ]
    }
</code>
<hr>
<code id="actual">
</code>

Answered by Ruben Helsloot on January 1, 2022

Add your own answers!

Related Questions

Vimeo offline video playback issue

0  Asked on September 26, 2020 by ipatel

         

How to print data from a few arrays

3  Asked on September 25, 2020 by protodimbo

   

why this.key is not working properly in javacript?

2  Asked on September 25, 2020 by carlos-daniel

 

Pass the dynamic variable when button is pressed

1  Asked on September 19, 2020 by francy

 

detect a table part from entire image in python

5  Asked on September 19, 2020 by suji

   

C# Change number of decimals show, but it always skips a number

0  Asked on September 18, 2020 by theoverly

   

Custom font bold weight not working in Dompdf

1  Asked on September 17, 2020 by user13286

   

How to use has one through in laravel?

0  Asked on September 17, 2020 by ahz

       

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir