1

I have 2 arrays, one (array1) which needs to be sorted based on its inner key, role, according to another (array2). I have tried different solutions but cannot progress any further since i don't understand what steps i should take

I have the following output: Array1

{
      "id":12,
      "roles":[
         {
            "id":12,
            "role":"team_player",
            "sub_role":null,
            "team_meta":{
               "default_player_role":{
                  "pos":null,
                  "role":"LWB"
               }
            }
         }
      ],
      "user_email":"[email protected]"
   },
   {
      "id":1575,
      "roles":[
         {
            "id":1672,
            "role":"team_player",
            "sub_role":null,
            "team_meta":{
               "default_player_role":{
                  "pos":null,
                  "role":"LB"
               }
            }
         }
      ],
      "user_email":"[email protected]"
   },
   {
      "id":1576,
      "roles":[
         {
            "id":1673,
            "role":"team_player",
            "sub_role":null,
            "team_meta":{
               "default_player_role":{
                  "pos":null,
                  "role":"CAM"
               }
            }
         }
      ],
      "user_email":"[email protected]",
   },

And i want to order the array above according to the order of this:

const array2 = ["LWB", "LB", "CAM"]

The issue i'm having is that the given key that the sorting should be according to in array1 is too deep, and I haven't found any way to map the "role" from the first array with the array2.

2 Answers 2

1

You need to get role and with this value get the index for the order.

const
    getRole = ({ roles: [{ team_meta: { default_player_role: { role } }}] }) => role,
    data = [{ id: 1576, roles: [{ id: 1673, role: "team_player", sub_role: null, team_meta: { default_player_role: { pos: null, role: "CAM" } } }], user_email: "[email protected]" }, { id: 12, roles: [{ id: 12, role: "team_player", sub_role: null, team_meta: { default_player_role: { pos: null, role: "LWB" } } }], user_email: "[email protected]" }, { id: 1575, roles: [{ id: 1672, role: "team_player", sub_role: null, team_meta: { default_player_role: { pos: null, role: "LB" } } }], user_email: "[email protected]" }],
    order = ["LWB", "LB", "CAM"];

data.sort((a, b) => order.indexOf(getRole(a)) - order.indexOf(getRole(b)));

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

2 Comments

Works fine in online compilers but surprisingly not in my environment. I get the following error: Types of property 'roles' are incompatible. [...] Target requires 1 element(s) but source may have fewer.
maybe you have a typed environment, then you need to adjust the type.
0

Over several loops you can also sort it but probably not an elegant solution:

const nestedArray = [...]; // Replace Array 
const sortByArray = ["LWB", "LB", "CAM"];
const sortedArray = [];

sortByArray.forEach(function(sortByArrayValue) {
    nestedArray.forEach(function(nestedArrayValue) {
        nestedArrayValue.roles.forEach(function(role) {
            if (role.team_meta.default_player_role.role === sortByArrayValue) {
                sortedArray.push(nestedArrayValue);
            }
        });
    });
});

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.