Home > Mobile >  Sort a nested array, given its inner value, based on another array
Sort a nested array, given its inner value, based on another array

Time:11-30

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.

CodePudding user response:

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

CodePudding user response:

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);
            }
        });
    });
});
  • Related