Home > Software design >  How to Convert object into array of object?
How to Convert object into array of object?

Time:12-01

I'm having object like this,

  const rolePermission = {
    adminView: true,
    adminCreate: true,
    adminDelete: true,
    userView: true,
    userEdit: true,
    userDelete: false,
  };

i expecting like this,

   const rolePermission = [
    { role: "admin", action: ["View", "Create", "Delete"] },
    { role: "user", action: ["View", "Edit"] },
  ];

CodePudding user response:

You can iterate over the properties and calculate the result from that.

const rolePermission = { adminView: true, adminCreate: true, adminDelete: true, userView: true, userEdit: true, userDelete: false };

// We want all the entries in `rolePermission`
const intermediate = Object
  .entries(rolePermission)
  .reduce((result, [key, enabled]) => {
    // Split the entry's key into the role and action, using a positive lookahead.
    const [role, action] = key.split(/(?=[A-Z])/);

    // If the action is enabled,
    if (enabled) {
      // Make sure the action array exists,
      result[role] = result[role] || [];
      // And add the current action to it.
      result[role].push(action);
    }

    return result;
  }, {});

console.log('intermediate:\n', intermediate);

// Now map those entries to the desired output format.
const permissions = Object
  .entries(intermediate)
  .map(([role, action]) => ({ role, action }));

console.log('result:\n', permissions);
.as-console-wrapper {
  max-height: 100% !important;
}

CodePudding user response:

  1. Iterate over the object with for...in.

  2. Use a regular expression to split each key into a role, and an action, and match the key with that. Create an empty object to update.

  3. If the empty object doesn't have a key that matches the current role, add it, and set it's value to an object with role and action properties - the action value is an empty array.

  4. If the value of the property in the current iteration identified by the key is true add that the action to the array.

  5. Use Object.values to get the array of objects from the updated object.

const rolePermission={adminView:!0,adminCreate:!0,adminDelete:!0,userView:!0,userEdit:!0,userDelete:!1};

// Match admin or user as one group, and the rest
// of the string as another group
const re = /^(admin|user)(. )$/;
const temp = {};

for (const key in rolePermission) {

  // When a match is made the role will be the first
  // element in the returned array, the action the second element
  const [role, action] = key.match(re).slice(1);

  // If the role doesn't exist in the temporary object
  // create it and assign a new default object to it
  temp[role] ??= { role, action: [] };

  // If the property value identified by the key
  // is true push the action to the actions array
  if (rolePermission[key]) {
    temp[role].action.push(action);
  }
}

// Get the array of objects
console.log(Object.values(temp));

CodePudding user response:

Use RegExp to get two match groups of role and action using the key of your object and do the filtering based on the value. Rest is how you get the desired output using Object.entries(), Array.prototype.map(), and Array.prototype.reduce()

This would also work:

const rolePermission = {
  adminView: true,
  adminCreate: true,
  adminDelete: true,
  userView: true,
  userEdit: true,
  userDelete: false,
};

const output = Object.entries(
  Object.entries(rolePermission)
    .map(([key, allowed]) => {
      const regex = new RegExp("(.*)(View|Edit|Create|Delete)", "g");
      const [, role, action] = regex.exec(key)
      return [role, action, allowed];
    }).filter(([,,allowed]) => allowed)
    .reduce((prev, [role, action]) => {
      prev[role] = prev[role] ? prev[role].concat([action]) : [action];
      return prev;
    }, {})
).map(([role, action]) => ({ role, action }));

console.log(output);

  • Related