0

I have got array of nested array of objects . I want to get only Permission objects from data and I need to convert data array to permission objects .

 "data":[
       {
          "Book":[
             {
                "label":"Can View",
                "value":"can_view"
             },
             {
                "label":"Can Create",
                "value":"can_create"
             },

          ]
       },
       {
          "Articles":[
             {
                "label":"Can View",
                "value":"can_view"
             },
         ]
       },
       {
          "Journals":[

             {
                "label":"Can Upload",
                "value":"can_upload"
             },
             {
                "label":"Can Download",
                "value":"can_download"
             }
          ]
       },

       {
          "Permission":[
             {
                "label":"Can View",
                "value":"can_view"
             },
             {
                "label":"Can Create",
                "value":"can_create"
             }
          ]
       }
    ]

I am trying to get permission objects from list of my data objects. I am using filter method for geting Permission array and want to convert permission objects, but I got nested arrays . where is the problem?

let dataPer = data.permissions.filter(item=>item.Permission).map(item=>item.Permission)

console.log(dataPer)

My accepted output would be :

 let output= {

       Permission:{
         can_view:"can_view",
       can_create:"can_create"

       }
   }   
5
  • 3
    accepted output is syntactically invalid. Commented Mar 26, 2020 at 14:13
  • The "accepted output" is not a valid object. Please at least do some research and testing before posting a question. Commented Mar 26, 2020 at 14:15
  • I edited . sorry it was mistake Commented Mar 26, 2020 at 14:18
  • Can you give a proper example of the kind of output you want ? Commented Mar 26, 2020 at 14:24
  • [{ "Permission":[ { "label":"Can View", "value":"can_view" }, { "label":"Can Create", "value":"can_create" } ] } ] I want to get output from this. my output would be : let output= { can_view:"can_view", can_create:"can_create" } Commented Mar 26, 2020 at 14:28

3 Answers 3

1

Well you could try something like this :

 let data=[
       {
          "Book":[
             {
                "label":"Can View",
                "value":"can_view"
             },
             {
                "label":"Can Create",
                "value":"can_create"
             }
          ]
       },
       {
          "Articles":[
             {
                "label":"Can View",
                "value":"can_view"
             }
         ]
       },
       {
          "Journals":[

             {
                "label":"Can Upload",
                "value":"can_upload"
             },
             {
                "label":"Can Download",
                "value":"can_download"
             }
          ]
       },

       {
          "Permission":[
             {
                "label":"Can View",
                "value":"can_view"
             },
             {
                "label":"Can Create",
                "value":"can_create"
             }
          ]
       }
    ];
let myPermissionObjects = [];
let permissionConvert = ()=>{
for(let current in data)
    for(let currentObject in data[current])
      for(let currentResult in data[current][currentObject])
        myPermissionObjects.push(data[current][currentObject][currentResult]);

};
permissionConvert();
console.log(myPermissionObjects);

if i understood your problem correctly

for the object permission only :

let data=[
           {
              "Book":[
                 {
                    "label":"Can View",
                    "value":"can_view"
                 },
                 {
                    "label":"Can Create",
                    "value":"can_create"
                 }
              ]
           },
           {
              "Articles":[
                 {
                    "label":"Can View",
                    "value":"can_view"
                 }
             ]
           },
           {
              "Journals":[

                 {
                    "label":"Can Upload",
                    "value":"can_upload"
                 },
                 {
                    "label":"Can Download",
                    "value":"can_download"
                 }
              ]
           },

           {
              "Permission":[
                 {
                    "label":"Can View",
                    "value":"can_view"
                 },
                 {
                    "label":"Can Create",
                    "value":"can_create"
                 }
              ]
           }
        ];
    let myPermissionObjects = [];
    let permissionConvert = ()=>{
    for(let current in data)
        for(let currentObject in data[current].Permission)
          myPermissionObjects.push( data[current].Permission[currentObject]);


    };
    permissionConvert();
    console.log(myPermissionObjects);
    let onePermission = [];
    for(let next in myPermissionObjects)
    {
    onePermission["Permission"]=myPermissionObjects[next];
    console.log(myPermissionObjects[next]);
    }

Sign up to request clarification or add additional context in comments.

1 Comment

I only need { "Permission":[ { "label":"Can View", "value":"can_view" }, { "label":"Can Create", "value":"can_create" } ] } this data
1

You can do it with reduce function, like this:

const data = [
  {
    Book: [
      {
        label: "Can View",
        value: "can_view"
      },
      {
        label: "Can Create",
        value: "can_create"
      }
    ]
  },
  {
    Articles: [
      {
        label: "Can View",
        value: "can_view"
      }
    ]
  },
  {
    Journals: [
      {
        label: "Can Upload",
        value: "can_upload"
      },
      {
        label: "Can Download",
        value: "can_download"
      }
    ]
  },

  {
    Permission: [
      {
        label: "Can View",
        value: "can_view"
      },
      {
        label: "Can Create",
        value: "can_create"
      }
    ]
  }
];

--

function getPermissionList() {
  const list = data
    .filter(item => item.Permission)[0]
    .Permission.reduce((list, item) => {
      list.push({ [item.label]: item.value });
      return list;
    }, {});
  return Object.assign({}, list);
}

Comments

0

I got the solution , but is it best for my code

let dataPer = data.permissions.filter(item=>item.Permission)
const result = Object.assign({},dataPer)

let res = result[0].Permission.map(item=>{
  return {
    [item.value]:item.value
  }
})
const obj = Object.assign({},res)
console.log(obj[0].can_view)

1 Comment

Depending on the operations you want to do with your object, yes it might be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.