-2

I want to map below mentioned object...

Input object:

const rules = {
    public: [
      { label: 'View', value: 'View' },
      { label: 'Create', value: 'Create' },
      { label: 'Delete', value: 'Delete' },
      { label: 'Update', value: 'Update' },
      { label: 'ChangeLayout', value: 'ChangeLayout' }
    ],
    user: [
      { label: 'View', value: 'View' },
      { label: 'Create', value: 'Create' },
      { label: 'Delete', value: 'Delete' },
      { label: 'Update', value: 'Update' },
      { label: 'ChangeLayout', value: 'ChangeLayout' }
    ]
  };

Expected output is:

 const result = {
        Public: { "View": true, "Create": false, "Delete": false, "Update": false, "ChangeLayout": false },
        User: { "View": true, "Create": true, "Delete": true, "Update": true, "ChangeLayout": true }
      };

Actually i try some method but cant getting expected output

Thanks to advance please help anyone...

Gowdham GM
  • 13
  • 6

3 Answers3

2

Please try to use below code

const rules = [
      { label: 'View', value: 'View' },
      { label: 'Create', value: 'Create' },
      { label: 'Delete', value: 'Delete' },
      { label: 'Update', value: 'Update' }
    ];

    let outPut = {};
    rules.forEach(item => {
      outPut[item.value] = true;
    })

    console.log(outPut);
  • FYI, OP hasn't tagged this with [tag:typescript] so they might find the `any` confusing – Phil Jul 29 '21 at 05:01
1

Your callback function is just returning a string, so map() is creating an array of strings, not an object.

The callback function should return an array containing the key and value. Then you can use Object.fromEntries() to turn that into the desired object.

const rules = {
    public: [
      { label: 'View', value: 'View' },
      { label: 'Create', value: 'Create' },
      { label: 'Delete', value: 'Delete' },
      { label: 'Update', value: 'Update' },
      { label: 'ChangeLayout', value: 'ChangeLayout' }
    ],
    user: [
      { label: 'View', value: 'View' },
      { label: 'Create', value: 'Create' },
      { label: 'Delete', value: 'Delete' },
      { label: 'Update', value: 'Update' },
      { label: 'ChangeLayout', value: 'ChangeLayout' }
    ]
};

const test = Object.fromEntries(Object.entries(rules).map(
  ([owner, rules]) => [owner, Object.fromEntries(
    rules.map(rule => [rule.value, true]))]));

console.log(test);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Hi please re-review the question , i did some corrections... – Gowdham GM Jul 29 '21 at 06:18
  • It's just another level of using `map` and `Object.fromEntries()`. This time it maps over `Object.entries()` since it's mapping over an object rather than an array. – Barmar Jul 29 '21 at 07:01
  • 1
    Do you really need to capitalize the first letter of `public` and `user`? I'll leave that as an exercise for you. See https://stackoverflow.com/questions/1026069/how-do-i-make-the-first-letter-of-a-string-uppercase-in-javascript – Barmar Jul 29 '21 at 07:03
0

To create a single object from your array, you can use a reduce operation, assigning the resulting key name via computed property names.

const rules = [{"label":"View","value":"View"},{"label":"Create","value":"Create"},{"label":"Delete","value":"Delete"},{"label":"Update","value":"Update"}]

const keyFrom = "value"

const t0 = performance.now()
const result = rules.reduce((obj, rule) => ({
  ...obj,
  [rule[keyFrom]]: true
}), {})
const t1 = performance.now()

console.log(result)
console.log(`Operation took ${t1 - t0}ms`)

I wasn't sure which property should provide the key name in your final object so I made it configurable via the keyFrom variable.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Hi please re-review the question , i did some corrections... – Gowdham GM Jul 29 '21 at 06:18
  • @GowdhamGM I don't know why you accepted this answer. I'm not going to update it to match your ever shifting requirements so you should probably just accept [Barmar's answer](https://stackoverflow.com/a/68570226/283366) instead – Phil Jul 29 '21 at 07:47
  • @Barmar this answer also helped for my project thats why... – Gowdham GM Jul 29 '21 at 08:03