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!

3
  • 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. Commented Feb 2, 2021 at 12:55
  • stackoverflow.com/questions/12303989/… seems like a good start. Some other options at stackoverflow.com/search?q=%5Bjavascript%5D+cartesian . Good luck! Commented Feb 2, 2021 at 12:58
  • @malarres yeah gone through those link they seems to applicable on arrays not object array thanks again for links tho ! cheers ! Commented Feb 2, 2021 at 13:05

2 Answers 2

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; }

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

Comments

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);

Comments

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.