Home > Blockchain >  Need help using Object.fromEntries to reorganize an array of objects. The fun stuff delimma
Need help using Object.fromEntries to reorganize an array of objects. The fun stuff delimma

Time:07-11

I have a form that accepts user selections from groups of options.

This is just an example of the objects I get from the db for the options.

let optiongroups = [
       {
        groupname : "exactly1",
        grouplimit : 1,
        id : 123,
        rules : "exactly",
        rulesnumber : {number :0, minimum : 0, maximum : 0 }, 
        optionitems : [
          {
            name : "One",
            price : 1
          },
          {
            name : "Two",
            price : 2
          }
        ]

       },
       {
        groupname : "exactly2",
        grouplimit : 2,
        id : 369,
        rules : "exactly",
        rulesnumber : {number :0, minimum : 0, maximum : 0 }, 
        optionitems : [
          {
            name : "2-One",
            price : 22
          },
          {
            name : "2-Two",
            price : 44
          }
        ]

       }]

I used Object.fromEntries to turn it to this:

finalgroupit = Object.fromEntries(optiongroups.map(g => [g.groupheader,  []])); 

So now I have an object that group them like this {"groupname1" : [], "groupname2" :[] }

That was good till I realized I need to validate user selection against the rules and rulesnumber before I submit the selection. That is my question. How to add rules and rulesnumber to my output in finalgroupit = Object.fromEntries()

I struggled with reduce() so after reading and researching I came accross the Object.fromEntries and using it with map() made really sense to me and I was able to reconstruct my data but I'm stuck with how to add rules and rulesnumber to the output for each group. Something like {"groupname" : [], grouprules : "", rulesnumber :""}

This whole thing with how to reconstruct the data to fit specific scenario is new to me and hopefully someone willing to help.

How do I do that?

CodePudding user response:

You can use Object.fromEntries, and with destructuring and sort ES6 object literal syntax, it becomes like this:

const optiongroups = [{groupname : "exactly1",grouplimit : 1,id : 123,rules : "exactly",rulesnumber : {number :0, minimum : 0, maximum : 0 },optionitems : [{name : "One",price : 1},{name : "Two",price : 2}]},{groupname : "exactly2",grouplimit : 2,id : 369,rules : "exactly",rulesnumber : {number :0, minimum : 0, maximum : 0 },optionitems : [{name : "2-One",price : 22},{name : "2-Two",price : 44}]}];

const result = Object.fromEntries(
    optiongroups.map(({groupname, rules, rulesnumber}) => 
        [groupname, { rules, rulesnumber, userSelections: [] }]
    )
);

console.log(result);

CodePudding user response:

let optiongroups = [
       {
        groupname : "exactly1",
        grouplimit : 1,
        id : 123,
        rules : "exactly",
        rulesnumber : {number :0, minimum : 0, maximum : 0 }, 
        optionitems : [
          {
            name : "One",
            price : 1
          },
          {
            name : "Two",
            price : 2
          }
        ]

       },
       {
        groupname : "exactly2",
        grouplimit : 2,
        id : 369,
        rules : "exactly",
        rulesnumber : {number :0, minimum : 0, maximum : 0 }, 
        optionitems : [
          {
            name : "2-One",
            price : 22
          },
          {
            name : "2-Two",
            price : 44
          }
        ]

       }];

const final = optiongroups.reduce((res, obj) => {
  res[obj.groupname] = {
    [obj.groupname]: [],
    "rules": obj.rules,
    "rulesnumber": obj.rulesnumber
  };
  
  return res;
}, {})
console.log(final);

let optiongroups = [
       {
        groupname : "exactly1",
        grouplimit : 1,
        id : 123,
        rules : "exactly",
        rulesnumber : {number :0, minimum : 0, maximum : 0 }, 
        optionitems : [
          {
            name : "One",
            price : 1
          },
          {
            name : "Two",
            price : 2
          }
        ]

       },
       {
        groupname : "exactly2",
        grouplimit : 2,
        id : 369,
        rules : "exactly",
        rulesnumber : {number :0, minimum : 0, maximum : 0 }, 
        optionitems : [
          {
            name : "2-One",
            price : 22
          },
          {
            name : "2-Two",
            price : 44
          }
        ]

       }];
       
const final = Object.fromEntries(optiongroups.map(g => {
  return [g.groupname,  
          {
            [g.groupname]: [],
            "rules": g.rules,
            "rulesnumber": g.rulesnumber
          }];
}));
console.log(final);

  • Related