___ ___ erkimt Sort an array within an object, entering all the hierarchies of the same ______ qstntxt ___

I own an object containing several arrays within it. And these arrays contain other objects that contain a %code% field that I would like under each hierarchy to be sorted by that field. And I would like to go through each sub-menu inside the objects and sort them as well.

%pre%     
______ azszpr252113 ___

Since %code% changes the array directly you can do this:

%pre%

Example:

%pre%
    
___

1

I own an object containing several arrays within it. And these arrays contain other objects that contain a lpec_sequence field that I would like under each hierarchy to be sorted by that field. And I would like to go through each sub-menu inside the objects and sort them as well.

{
  "layout": {
    "menu_events": [
      {
    "lpec_id": 90,
    "lpec_sequence": 1,
    "sub_menu": false
      },
      {
    "lpec_id": 125,
    "lpec_sequence": 2,
    "sub_menu": false
      },
      {
    "lpec_id": 124,
    "lpec_sequence": 3,
    "sub_menu": true,
    "sub_menu_items": [
      {
        "lpec_id": 96,
        "lpec_sequence": 5,
        "sub_menu": false
      },
      {
        "lpec_id": 97,
        "lpec_sequence": 6,
        "sub_menu": false
      },
      {
        "lpec_id": 98,
        "lpec_sequence": 7,
        "sub_menu": false
      },
      {
        "lpec_id": 99,
        "lpec_sequence": 8,
        "sub_menu": true,
        "sub_menu_items": [
          {
            "lpec_id": 100,
            "lpec_sequence": 1,
            "sub_menu": false
          },
          {
            "lpec_id": 101,
            "lpec_sequence": 2,
            "sub_menu": false
          },
          {
            "lpec_id": 102,
            "lpec_sequence": 3,
            "sub_menu": false
          },
          {
            "lpec_id": 103,
            "lpec_sequence": 4,
            "sub_menu": false
          },
          {
            "lpec_id": 104,
            "lpec_sequence": 5,
            "sub_menu": false
          },
          {
            "lpec_id": 105,
            "lpec_sequence": 6,
            "sub_menu": false
          },
          {
            "lpec_id": 106,
            "lpec_sequence": 7,
            "sub_menu": false
          }
        ]
      },
      {
        "lpec_id": 118,
        "lpec_sequence": 10,
        "sub_menu": true,
        "sub_menu_items": [
          {
            "lpec_id": 121,
            "lpec_sequence": 3,
            "sub_menu": false
          },
          {
            "lpec_id": 122,
            "lpec_sequence": 4,
            "sub_menu": false
          },
          {
            "lpec_id": 119,
            "lpec_sequence": 1,
            "sub_menu": false
          },
          {
            "lpec_id": 120,
            "lpec_sequence": 2,
            "sub_menu": false
          }
        ]
      },
      {
        "lpec_id": 92,
        "lpec_sequence": 1,
        "sub_menu": false
      },
      {
        "lpec_id": 93,
        "lpec_sequence": 2,
        "sub_menu": false
      },
      {
        "lpec_id": 94,
        "lpec_sequence": 3,
        "sub_menu": false
      },
      {
        "lpec_id": 95,
        "lpec_sequence": 4,
        "sub_menu": false
      },
      {
        "lpec_id": 107,
        "lpec_sequence": 9,
        "sub_menu": true,
        "sub_menu_items": [
          {
            "lpec_id": 109,
            "lpec_sequence": 2,
            "sub_menu": false
          },
          {
            "lpec_id": 110,
            "lpec_sequence": 3,
            "sub_menu": false
          },
          {
            "lpec_id": 111,
            "lpec_sequence": 4,
            "sub_menu": false
          },
          {
            "lpec_id": 112,
            "lpec_sequence": 5,
            "sub_menu": false
          },
          {
            "lpec_id": 114,
            "lpec_sequence": 7,
            "sub_menu": false
          },
          {
            "lpec_id": 113,
            "lpec_sequence": 6,
            "sub_menu": false
          },
          {
            "lpec_id": 115,
            "lpec_sequence": 8,
            "sub_menu": false
          },
          {
            "lpec_id": 116,
            "lpec_sequence": 9,
            "sub_menu": false
          },
          {
            "lpec_id": 117,
            "lpec_sequence": 10,
            "sub_menu": false
          },
          {
            "lpec_id": 108,
            "lpec_sequence": 1,
            "sub_menu": false
          }
        ]
      }
    ]
      },
      {
    "lpec_id": 123,
    "lpec_sequence": 2,
    "sub_menu": true,
    "sub_menu_items": [
      {
        "lpec_id": 91,
        "lpec_sequence": 1,
        "sub_menu": false
      }
    ]
      }
    ]
  }
}
    
asked by anonymous 03.11.2017 / 11:56

1 answer

2

Since .sort() changes the array directly you can do this:

// ordenar primeiro os sub menus
data.layout.menu_events.forEach(
  event => event.sub_menu_items && event.sub_menu_items.sort((a, b) => a.lpec_id - b.lpec_id)
);

// ordenar os menus com os sub menus já ordenados
data.layout.menu_events.sort(
  (a, b) => a.lpec_sequence - b.lpec_sequence
);

Example:

var data = {
  "layout": {
    "menu_events": [{
        "lpec_id": 90,
        "lpec_sequence": 1,
        "sub_menu": false
      },
      {
        "lpec_id": 125,
        "lpec_sequence": 2,
        "sub_menu": false
      },
      {
        "lpec_id": 124,
        "lpec_sequence": 3,
        "sub_menu": true,
        "sub_menu_items": [{
            "lpec_id": 96,
            "lpec_sequence": 5,
            "sub_menu": false
          },
          {
            "lpec_id": 97,
            "lpec_sequence": 6,
            "sub_menu": false
          },
          {
            "lpec_id": 98,
            "lpec_sequence": 7,
            "sub_menu": false
          },
          {
            "lpec_id": 99,
            "lpec_sequence": 8,
            "sub_menu": true,
            "sub_menu_items": [{
                "lpec_id": 100,
                "lpec_sequence": 1,
                "sub_menu": false
              },
              {
                "lpec_id": 101,
                "lpec_sequence": 2,
                "sub_menu": false
              },
              {
                "lpec_id": 102,
                "lpec_sequence": 3,
                "sub_menu": false
              },
              {
                "lpec_id": 103,
                "lpec_sequence": 4,
                "sub_menu": false
              },
              {
                "lpec_id": 104,
                "lpec_sequence": 5,
                "sub_menu": false
              },
              {
                "lpec_id": 105,
                "lpec_sequence": 6,
                "sub_menu": false
              },
              {
                "lpec_id": 106,
                "lpec_sequence": 7,
                "sub_menu": false
              }
            ]
          },
          {
            "lpec_id": 118,
            "lpec_sequence": 10,
            "sub_menu": true,
            "sub_menu_items": [{
                "lpec_id": 121,
                "lpec_sequence": 3,
                "sub_menu": false
              },
              {
                "lpec_id": 122,
                "lpec_sequence": 4,
                "sub_menu": false
              },
              {
                "lpec_id": 119,
                "lpec_sequence": 1,
                "sub_menu": false
              },
              {
                "lpec_id": 120,
                "lpec_sequence": 2,
                "sub_menu": false
              }
            ]
          },
          {
            "lpec_id": 92,
            "lpec_sequence": 1,
            "sub_menu": false
          },
          {
            "lpec_id": 93,
            "lpec_sequence": 2,
            "sub_menu": false
          },
          {
            "lpec_id": 94,
            "lpec_sequence": 3,
            "sub_menu": false
          },
          {
            "lpec_id": 95,
            "lpec_sequence": 4,
            "sub_menu": false
          },
          {
            "lpec_id": 107,
            "lpec_sequence": 9,
            "sub_menu": true,
            "sub_menu_items": [{
                "lpec_id": 109,
                "lpec_sequence": 2,
                "sub_menu": false
              },
              {
                "lpec_id": 110,
                "lpec_sequence": 3,
                "sub_menu": false
              },
              {
                "lpec_id": 111,
                "lpec_sequence": 4,
                "sub_menu": false
              },
              {
                "lpec_id": 112,
                "lpec_sequence": 5,
                "sub_menu": false
              },
              {
                "lpec_id": 114,
                "lpec_sequence": 7,
                "sub_menu": false
              },
              {
                "lpec_id": 113,
                "lpec_sequence": 6,
                "sub_menu": false
              },
              {
                "lpec_id": 115,
                "lpec_sequence": 8,
                "sub_menu": false
              },
              {
                "lpec_id": 116,
                "lpec_sequence": 9,
                "sub_menu": false
              },
              {
                "lpec_id": 117,
                "lpec_sequence": 10,
                "sub_menu": false
              },
              {
                "lpec_id": 108,
                "lpec_sequence": 1,
                "sub_menu": false
              }
            ]
          }
        ]
      },
      {
        "lpec_id": 123,
        "lpec_sequence": 2,
        "sub_menu": true,
        "sub_menu_items": [{
          "lpec_id": 91,
          "lpec_sequence": 1,
          "sub_menu": false
        }]
      }
    ]
  }
}

function sortEfter(obj, key, subKey) {
  if (!obj[subKey]) return;
  obj[subKey] = obj[subKey].sort((a, b) => a[key] - b[key]);
  obj[subKey].forEach(el => sortEfter(el, key, subKey))
}

data.layout.menu_events.forEach(
  event => sortEfter(event, 'lpec_id', 'sub_menu_items')
);

data.layout.menu_events.sort(
  (a, b) => a.lpec_sequence - b.lpec_sequence
);

console.log(data.layout.menu_events);
    
03.11.2017 / 12:39