简体   繁体   中英

How to segregate an array of object by using map?

I have to form an array of object in another array of object based on id. I was able to group the object based the "applicationId" but was not able to group the inside array of attributes and values array. Also the above code gives me duplicate objects. Please help with this I know its a small fix but I spend whole day still no result. stackblitz . Expected output commented below in stackblitz

 data.map((el) => {
      el.attribute.map((elm) => {
        elm.options.map(em => {
          permissions.push({
            applicationId: el.application, attributes: [{ name: elm.name, value: em.value, disabled: true 
          }]
          })
        })
      })
    });

Input Object

[
    {
        application: "abc", 
        attribute: [
            {
                description: "abc description1"
                name: "audio"
                options:[
                    {name: "Yes", value: "Y"}
                    {name: "No", value: "N"}
                ]
            },
            {
                description: "abc description2"
                name: "video"
                options:[
                    {name: "true", value: "T"}
                    {name: "false", value: "F"}
                ]
            }
            {
                description: "abc description3"
                name: "call"
                options:[
                    {name: "Yes", value: "Y"}
                    {name: "false", value: "F"}
                ]
            }
        ]
    },
    {
        application: "def", 
        attribute: [
            {
                description: "def description1"
                name: "audio"
                options:[
                    {name: "Yes", value: "Y"}
                    {name: "No", value: "N"}
                ]
            },
            {
                description: "def description2"
                name: "video"
                options:[
                    {name: "true", value: "T"}
                    {name: "false", value: "F"}
                ]
            }
            {
                description: "def description3"
                name: "call"
                options:[
                    {name: "Yes", value: "Y"}
                    {name: "false", value: "F"}
                ]
            }
        ]
    }
]

Expected Output:

permissions:[
    {
    applicationId:abc
    attributes:
        [
            {
            name:audio
            value:["Y","N"]
            disabled: true
            },
            {
            name:video,
            value:["T","F"]
            disabled: true
            },
            {
            name:call,
            value:["Y","F"]
            disabled: true
            }
        ]
    },
    {
    applicationId: def
    attributes:
        [
            {
            name:audio
            value:["Y","N"]
            disabled: true
            },
            {
            name:video,
            value:["T","F"]
            disabled: true
            },
            {
            name:call,
            value:["Y","F"]
            disabled: true
            }
        ]
    }
]

You could do so using few array map s.

Try the following

 var input = [ { application: "abc", attribute: [ { description: "abc description1", name: "audio", options: [ { name: "Yes", value: "Y" }, { name: "No", value: "N" } ] }, { description: "abc description2", name: "video", options: [ { name: "true", value: "T" }, { name: "false", value: "F" } ] }, { description: "abc description3", name: "call", options: [ { name: "Yes", value: "Y" }, { name: "false", value: "F" } ] } ] }, { application: "def", attribute: [ { description: "def description1", name: "audio", options: [ { name: "Yes", value: "Y" }, { name: "No", value: "N" } ] }, { description: "def description2", name: "video", options: [ { name: "true", value: "T" }, { name: "false", value: "F" } ] }, { description: "def description3", name: "call", options: [ { name: "Yes", value: "Y" }, { name: "false", value: "F" } ] } ] } ]; var output = input.map(item => ({ applicationId: item.application, attributes: item.attribute.map(attr => ({ name: attr.name, value: attr.options.map(option => option.value), disabled: true })) })); console.log(output);

I've modified your Stackblitz .


Sidenote: Please try to post a valid object in the question. It'll make it much easier and quicker to reproduce the issue. Much of my time was spent not in the solution but in fixing the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM