Skip to content Skip to sidebar Skip to footer

Create New Multidimensional Array Based On This Existing Array Objec

I'm stuck to construct a new multidimensional array based on this existing array object acl:[ { view:true, update:true, remove:true, userId:1, username:'Mike' },

Solution 1:

You can use reduce to group into an object of arrays:

const acl=[{view:!0,update:!0,remove:!0,userId:1,username:"Mike"},{view:!0,update:!0,remove:!1,userId:2,username:"Joe"},{view:!0,update:!1,remove:!1,userId:3,username:"Lim"}];

console.log(
  acl.reduce((a, { view, update, remove, userId, username }) => {
    const obj = { username, userId };
    if (view) a.view.push(obj);
    if (update) a.update.push(obj);
    if (remove) a.remove.push(obj);
    return a;
  }, { view: [], update: [], remove: [] })
);

Or, if the username / userId objects need to be separate references, you can use spread:

const acl=[{view:!0,update:!0,remove:!0,userId:1,username:"Mike"},{view:!0,update:!0,remove:!1,userId:2,username:"Joe"},{view:!0,update:!1,remove:!1,userId:3,username:"Lim"}];

console.log(
  acl.reduce((a, { view, update, remove, userId, username }) => {
    const obj = { username, userId };
    if (view) a.view.push({...obj});
    if (update) a.update.push({...obj});
    if (remove) a.remove.push({...obj});
    return a;
  }, { view: [], update: [], remove: [] })
);

Solution 2:

Normally I wouldn't answer this but the number of people telling you to use reduce is killing me.

The neatest way to do this (if you're only working with view/update/remove) would be to use filters. They actually describe the action you're trying to take.

const acl2 = {
  view: acl.filter(({view}) => view),
  update: acl.filter(({update}) => update),
  remove: acl.filter(({remove}) => remove),
};

Solution 3:

const acl = [{"view":true,"update":true,"remove":true,"userId":1,"username":"Mike"},{"view":true,"update":true,"remove":false,"userId":2,"username":"Joe"},{"view":true,"update":false,"remove":false,"userId":3,"username":"Lim"}]

functionmulti_dim (array) {

  const res = {
    view: [],
    update: [],
    remove: []
  }

  const keys = Object.keys(array[0])

  for (let i = 0; i < array.length; i++) {
    keys.forEach(function (key) {
      if (key !== 'username' && key !== 'userId') {
        if (array[i][key]) {
          res[key].push({
            username: array[i].username,
            userId: array[i].userId
          })
        }
      }
    })
  }

  return res

}

let obj = multi_dim(acl) // returns object. Push to array if so desired

console.log(obj)

// => result//   {//     "view": [//       {//         "username": "Mike",//         "userId": 1//       },//       {//         "username": "Joe",//         "userId": 2//       },//       {//         "username": "Lim",//         "userId": 3//       }//     ],//     "update": [//       {//         "username": "Mike",//         "userId": 1//       },//       {//         "username": "Joe",//         "userId": 2//       }//     ],//     "remove": [//       {//         "username": "Mike",//         "userId": 1//       }//     ]//   }

Solution 4:

You can use Array.prototype.reduce() as follows:

const permissions = ['view', 'update', 'remove'];
const output = acl.reduce((a, v) => {
  permissions.forEach(p => {
    if (v[p]) a[p].push({username: v.username, userId: v.userId});
  });

  return a;
}, permissions.reduce((a, p) => ({ ...a, [p]: []}), {}));

const acl = [{
    view: true,
    update: true,
    remove: true,
    userId: 1,
    username: "Mike"
  },
  {
    view: true,
    update: true,
    remove: false,
    userId: 2,
    username: "Joe"
  },
  {
    view: true,
    update: false,
    remove: false,
    userId: 3,
    username: "Lim"
  }];

const permissions = ['view', 'update', 'remove'];

const output = acl.reduce((a, v) => {
  permissions.forEach(p => {
    if (v[p]) a[p].push({username: v.username, userId: v.userId});
  });
  
  return a;
}, permissions.reduce((a, p) => ({ ...a, [p]: []}), {}));

console.log(output);

Post a Comment for "Create New Multidimensional Array Based On This Existing Array Objec"