0

I have below array of objects

{
 "roleid":[{"rolename":[1639]}],
 "modnameid":[{"mname":[1516,1515,1514]}],
 "accesstype":[{"accesstype":["VO","AA"]}]
}

and I want to convert it format below

[
{"modnameid":1516,"accesstype":"VO","roleid":1639},
{"modnameid":1516,"accesstype":"AA","roleid":1639},
{"modnameid":1515,"accesstype":"VO","roleid":1639},
{"modnameid":1515,"accesstype":"AA","roleid":1639},
{"modnameid":1514,"accesstype":"VO","roleid":1639},
{"modnameid":1514,"accesstype":"AA","roleid":1639}
]

How do we go about it, basic assumption what multi-tier map based operation, food for thoughts Assistance is appreciated!

Rizwan Patel
  • 538
  • 2
  • 9
  • 27
  • 3
    Sorry, you need to try it yourself first. Please show your attempt and explain exactly where you're stuck and where the code you wrote isn't behaving as intended. Try some `.forEach()`, some `.map()`, then we'll see where you're stuck. – Jeremy Thille Feb 02 '21 at 12:55
  • https://stackoverflow.com/questions/12303989/cartesian-product-of-multiple-arrays-in-javascript seems like a good start. Some other options at https://stackoverflow.com/search?q=%5Bjavascript%5D+cartesian . Good luck! – malarres Feb 02 '21 at 12:58
  • @malarres yeah gone through those link they seems to applicable on arrays not object array thanks again for links tho ! cheers ! – Rizwan Patel Feb 02 '21 at 13:05

2 Answers2

1

can user

let data = {
    "roleid": [{ "rolename": [1639] }],
    "modnameid": [{ "mname": [1516, 1515, 1514] }],
    "accesstype": [{ "accesstype": ["VO", "AA"] }]
}
let roles = []
data.roleid.forEach(element => {
    roles = [...roles, ...element.rolename]
});

let names = []
data.modnameid.forEach(element => {
    names = [...names, ...element.mname]
});

let types = []
data.accesstype.forEach(element => {
    types = [...types, ...element.accesstype]
});

let informations = []
roles.forEach(role => {
    names.forEach(name => {
        types.forEach(type => {
            informations.push({ "modnameid": role, "accesstype": type, "roleid": name })
        });
    });
});

console.log(informations);
1

Beside the hardcoded answer, you could take a dynamic approach and get first an object with flat arrays of data and build an array of the cartesian product.

function getCartesian(object) {
    return Object.entries(object).reduce((r, [k, v]) => {
        var temp = [];
        r.forEach(s =>
            (Array.isArray(v) ? v : [v]).forEach(w =>
                (w && typeof w === 'object' ? getCartesian(w) : [w]).forEach(x =>
                    temp.push(Object.assign({}, s, { [k]: x }))
                )
            )
        );
        return temp;
    }, [{}]);
}

const
    data = { roleid: [{ rolename: [1639] }], modnameid: [{ mname: [1516, 1515, 1514] }], accesstype: [{ accesstype: ["VO", "AA"] }] },
    flat = v => v && typeof v === 'object'
        ? Object.values(v).flatMap(flat)
        : v;
    temp = Object.fromEntries(Object.entries(data).map(([k, v]) => [k, flat(v)]));

console.log(temp);
console.log(getCartesian(temp));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392