Multidimensional array of an unknown size in JavaScript












2















Really struggling to get my head around this...
I'm trying to sort items from an array into another array, but categorised. My code is currently working, but the trouble is that I need to know the size of the "sort" array. At the moment, there are four entries, which works with "data". The first two data.cat are compared to n, and their IDs are pushed into sort[0]. When two do not match, 1 is added to c. The code then loops through again, pushing the new matches into sort[1], etc, etc.



var data = [
{"cat": 0, "id":"AAAA"},
{"cat": 0, "id":"BBBB"},
{"cat": 1, "id":"CCCC"},
{"cat": 1, "id":"DDDD"},
{"cat": 1, "id":"EEEE"},
{"cat": 1, "id":"FFFF"}, //pseudodata
{"cat": 1, "id":"GGGG"},
{"cat": 2, "id":"HHHH"},
{"cat": 2, "id":"IIII"},
{"cat": 2, "id":"JJJJ"},
{"cat": 3, "id":"KKKK"}
];

var sort = [, , , ,]

function sortDates(){
var n = 0;
for (var i = 0; i < data.length; i++){
if (data[i].cat == n){
console.log("Category " + n +" entry: " + data[i].id);
sort[n].push(data[i].id);
} else {
console.log("Entry " + data[i].id + " does not match. Moving to next category...");
n++;
i--;
}
}
}


This all works okay, but if I add more to the data array (such as {"cat": 4, "id":"LLLL"}), the program crashes with "sort[n] is not a function". This is because there are only four available items in the sort array.



So I'm just wondering, is there any way to get around this? If the data array is always changing size, and more entries/categories are added, do I have to keep resizing the sort array manually?










share|improve this question



























    2















    Really struggling to get my head around this...
    I'm trying to sort items from an array into another array, but categorised. My code is currently working, but the trouble is that I need to know the size of the "sort" array. At the moment, there are four entries, which works with "data". The first two data.cat are compared to n, and their IDs are pushed into sort[0]. When two do not match, 1 is added to c. The code then loops through again, pushing the new matches into sort[1], etc, etc.



    var data = [
    {"cat": 0, "id":"AAAA"},
    {"cat": 0, "id":"BBBB"},
    {"cat": 1, "id":"CCCC"},
    {"cat": 1, "id":"DDDD"},
    {"cat": 1, "id":"EEEE"},
    {"cat": 1, "id":"FFFF"}, //pseudodata
    {"cat": 1, "id":"GGGG"},
    {"cat": 2, "id":"HHHH"},
    {"cat": 2, "id":"IIII"},
    {"cat": 2, "id":"JJJJ"},
    {"cat": 3, "id":"KKKK"}
    ];

    var sort = [, , , ,]

    function sortDates(){
    var n = 0;
    for (var i = 0; i < data.length; i++){
    if (data[i].cat == n){
    console.log("Category " + n +" entry: " + data[i].id);
    sort[n].push(data[i].id);
    } else {
    console.log("Entry " + data[i].id + " does not match. Moving to next category...");
    n++;
    i--;
    }
    }
    }


    This all works okay, but if I add more to the data array (such as {"cat": 4, "id":"LLLL"}), the program crashes with "sort[n] is not a function". This is because there are only four available items in the sort array.



    So I'm just wondering, is there any way to get around this? If the data array is always changing size, and more entries/categories are added, do I have to keep resizing the sort array manually?










    share|improve this question

























      2












      2








      2








      Really struggling to get my head around this...
      I'm trying to sort items from an array into another array, but categorised. My code is currently working, but the trouble is that I need to know the size of the "sort" array. At the moment, there are four entries, which works with "data". The first two data.cat are compared to n, and their IDs are pushed into sort[0]. When two do not match, 1 is added to c. The code then loops through again, pushing the new matches into sort[1], etc, etc.



      var data = [
      {"cat": 0, "id":"AAAA"},
      {"cat": 0, "id":"BBBB"},
      {"cat": 1, "id":"CCCC"},
      {"cat": 1, "id":"DDDD"},
      {"cat": 1, "id":"EEEE"},
      {"cat": 1, "id":"FFFF"}, //pseudodata
      {"cat": 1, "id":"GGGG"},
      {"cat": 2, "id":"HHHH"},
      {"cat": 2, "id":"IIII"},
      {"cat": 2, "id":"JJJJ"},
      {"cat": 3, "id":"KKKK"}
      ];

      var sort = [, , , ,]

      function sortDates(){
      var n = 0;
      for (var i = 0; i < data.length; i++){
      if (data[i].cat == n){
      console.log("Category " + n +" entry: " + data[i].id);
      sort[n].push(data[i].id);
      } else {
      console.log("Entry " + data[i].id + " does not match. Moving to next category...");
      n++;
      i--;
      }
      }
      }


      This all works okay, but if I add more to the data array (such as {"cat": 4, "id":"LLLL"}), the program crashes with "sort[n] is not a function". This is because there are only four available items in the sort array.



      So I'm just wondering, is there any way to get around this? If the data array is always changing size, and more entries/categories are added, do I have to keep resizing the sort array manually?










      share|improve this question














      Really struggling to get my head around this...
      I'm trying to sort items from an array into another array, but categorised. My code is currently working, but the trouble is that I need to know the size of the "sort" array. At the moment, there are four entries, which works with "data". The first two data.cat are compared to n, and their IDs are pushed into sort[0]. When two do not match, 1 is added to c. The code then loops through again, pushing the new matches into sort[1], etc, etc.



      var data = [
      {"cat": 0, "id":"AAAA"},
      {"cat": 0, "id":"BBBB"},
      {"cat": 1, "id":"CCCC"},
      {"cat": 1, "id":"DDDD"},
      {"cat": 1, "id":"EEEE"},
      {"cat": 1, "id":"FFFF"}, //pseudodata
      {"cat": 1, "id":"GGGG"},
      {"cat": 2, "id":"HHHH"},
      {"cat": 2, "id":"IIII"},
      {"cat": 2, "id":"JJJJ"},
      {"cat": 3, "id":"KKKK"}
      ];

      var sort = [, , , ,]

      function sortDates(){
      var n = 0;
      for (var i = 0; i < data.length; i++){
      if (data[i].cat == n){
      console.log("Category " + n +" entry: " + data[i].id);
      sort[n].push(data[i].id);
      } else {
      console.log("Entry " + data[i].id + " does not match. Moving to next category...");
      n++;
      i--;
      }
      }
      }


      This all works okay, but if I add more to the data array (such as {"cat": 4, "id":"LLLL"}), the program crashes with "sort[n] is not a function". This is because there are only four available items in the sort array.



      So I'm just wondering, is there any way to get around this? If the data array is always changing size, and more entries/categories are added, do I have to keep resizing the sort array manually?







      javascript arrays loops multidimensional-array 2d






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 25 '18 at 19:20









      default-LAdefault-LA

      3517




      3517
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You could use a default check of the array with a logical OR || and assign an empty array if the item is not set.



          sort[n] = sort[n] || ;
          sort[n].push(data[i].id);


          BTW, you could use cat directly without iterating sort array for getting the right index.






          function sortDates() {
          var i, sort = ;
          for (i = 0; i < data.length; i++) {
          sort[data[i].cat] = sort[data[i].cat] || ;
          sort[data[i].cat].push(data[i].id);
          }
          return sort;
          }

          var data = [{ cat: 0, id: "AAAA" }, { cat: 0, id: "BBBB" }, { cat: 1, id: "CCCC" }, { cat: 1, id: "DDDD" }, { cat: 1, id: "EEEE" }, { cat: 1, id: "FFFF" }, { cat: 1, id: "GGGG" }, { cat: 2, id: "HHHH" }, { cat: 2, id: "IIII" }, { cat: 2, id: "JJJJ" }, { cat: 3, id: "KKKK" }],
          result = sortDates(data);

          console.log(result);

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








          share|improve this answer


























          • Absolutely perfect - thank you! I knew there was a more efficient way to do this... so the line: sort[data[i].cat] = sort[data[i].cat] || ; Is this basically saying, push data[i] to sort[data[i].cat], or if sort[data[i].cat doesn't exist, use the logic OR, which pushes it into a new item in sort?

            – default-LA
            Nov 25 '18 at 20:38











          • right first the check and if not a truthy value, like an array, then assign an array and later push the new value.

            – Nina Scholz
            Nov 25 '18 at 20:41



















          0














          Another way you can achieve this with ES6 in a more concise manner is to use Array.reduce and inside Array.sort via String.localeCompare:






          var data = [{ "cat": 0, "id": "BBBB" }, { "cat": 0, "id": "AAAA" }, { "cat": 1, "id": "CCCC" }, { "cat": 1, "id": "DDDD" }, { "cat": 1, "id": "EEEE" }, { "cat": 1, "id": "FFFF" }, { "cat": 1, "id": "GGGG" }, { "cat": 2, "id": "HHHH" }, { "cat": 2, "id": "IIII" }, { "cat": 2, "id": "JJJJ" }, { "cat": 3, "id": "KKKK" } ]

          const result = Object.values(data.reduce((r,c) => {
          r[c.cat] = [...r[c.cat] || , c.id].sort((a,b) => a.localeCompare(b))
          return r
          }, {}))
          console.log(result)





          String.localeCompare has various options in terms of how to compare (like dealing with numbers in the strings, case sensitivity etc)



          The idea is to use the Array.reduce is to group the entries in an object and then just use Object.values to get the desired output (milti-dimentional array).






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471015%2fmultidimensional-array-of-an-unknown-size-in-javascript%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You could use a default check of the array with a logical OR || and assign an empty array if the item is not set.



            sort[n] = sort[n] || ;
            sort[n].push(data[i].id);


            BTW, you could use cat directly without iterating sort array for getting the right index.






            function sortDates() {
            var i, sort = ;
            for (i = 0; i < data.length; i++) {
            sort[data[i].cat] = sort[data[i].cat] || ;
            sort[data[i].cat].push(data[i].id);
            }
            return sort;
            }

            var data = [{ cat: 0, id: "AAAA" }, { cat: 0, id: "BBBB" }, { cat: 1, id: "CCCC" }, { cat: 1, id: "DDDD" }, { cat: 1, id: "EEEE" }, { cat: 1, id: "FFFF" }, { cat: 1, id: "GGGG" }, { cat: 2, id: "HHHH" }, { cat: 2, id: "IIII" }, { cat: 2, id: "JJJJ" }, { cat: 3, id: "KKKK" }],
            result = sortDates(data);

            console.log(result);

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








            share|improve this answer


























            • Absolutely perfect - thank you! I knew there was a more efficient way to do this... so the line: sort[data[i].cat] = sort[data[i].cat] || ; Is this basically saying, push data[i] to sort[data[i].cat], or if sort[data[i].cat doesn't exist, use the logic OR, which pushes it into a new item in sort?

              – default-LA
              Nov 25 '18 at 20:38











            • right first the check and if not a truthy value, like an array, then assign an array and later push the new value.

              – Nina Scholz
              Nov 25 '18 at 20:41
















            1














            You could use a default check of the array with a logical OR || and assign an empty array if the item is not set.



            sort[n] = sort[n] || ;
            sort[n].push(data[i].id);


            BTW, you could use cat directly without iterating sort array for getting the right index.






            function sortDates() {
            var i, sort = ;
            for (i = 0; i < data.length; i++) {
            sort[data[i].cat] = sort[data[i].cat] || ;
            sort[data[i].cat].push(data[i].id);
            }
            return sort;
            }

            var data = [{ cat: 0, id: "AAAA" }, { cat: 0, id: "BBBB" }, { cat: 1, id: "CCCC" }, { cat: 1, id: "DDDD" }, { cat: 1, id: "EEEE" }, { cat: 1, id: "FFFF" }, { cat: 1, id: "GGGG" }, { cat: 2, id: "HHHH" }, { cat: 2, id: "IIII" }, { cat: 2, id: "JJJJ" }, { cat: 3, id: "KKKK" }],
            result = sortDates(data);

            console.log(result);

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








            share|improve this answer


























            • Absolutely perfect - thank you! I knew there was a more efficient way to do this... so the line: sort[data[i].cat] = sort[data[i].cat] || ; Is this basically saying, push data[i] to sort[data[i].cat], or if sort[data[i].cat doesn't exist, use the logic OR, which pushes it into a new item in sort?

              – default-LA
              Nov 25 '18 at 20:38











            • right first the check and if not a truthy value, like an array, then assign an array and later push the new value.

              – Nina Scholz
              Nov 25 '18 at 20:41














            1












            1








            1







            You could use a default check of the array with a logical OR || and assign an empty array if the item is not set.



            sort[n] = sort[n] || ;
            sort[n].push(data[i].id);


            BTW, you could use cat directly without iterating sort array for getting the right index.






            function sortDates() {
            var i, sort = ;
            for (i = 0; i < data.length; i++) {
            sort[data[i].cat] = sort[data[i].cat] || ;
            sort[data[i].cat].push(data[i].id);
            }
            return sort;
            }

            var data = [{ cat: 0, id: "AAAA" }, { cat: 0, id: "BBBB" }, { cat: 1, id: "CCCC" }, { cat: 1, id: "DDDD" }, { cat: 1, id: "EEEE" }, { cat: 1, id: "FFFF" }, { cat: 1, id: "GGGG" }, { cat: 2, id: "HHHH" }, { cat: 2, id: "IIII" }, { cat: 2, id: "JJJJ" }, { cat: 3, id: "KKKK" }],
            result = sortDates(data);

            console.log(result);

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








            share|improve this answer















            You could use a default check of the array with a logical OR || and assign an empty array if the item is not set.



            sort[n] = sort[n] || ;
            sort[n].push(data[i].id);


            BTW, you could use cat directly without iterating sort array for getting the right index.






            function sortDates() {
            var i, sort = ;
            for (i = 0; i < data.length; i++) {
            sort[data[i].cat] = sort[data[i].cat] || ;
            sort[data[i].cat].push(data[i].id);
            }
            return sort;
            }

            var data = [{ cat: 0, id: "AAAA" }, { cat: 0, id: "BBBB" }, { cat: 1, id: "CCCC" }, { cat: 1, id: "DDDD" }, { cat: 1, id: "EEEE" }, { cat: 1, id: "FFFF" }, { cat: 1, id: "GGGG" }, { cat: 2, id: "HHHH" }, { cat: 2, id: "IIII" }, { cat: 2, id: "JJJJ" }, { cat: 3, id: "KKKK" }],
            result = sortDates(data);

            console.log(result);

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








            function sortDates() {
            var i, sort = ;
            for (i = 0; i < data.length; i++) {
            sort[data[i].cat] = sort[data[i].cat] || ;
            sort[data[i].cat].push(data[i].id);
            }
            return sort;
            }

            var data = [{ cat: 0, id: "AAAA" }, { cat: 0, id: "BBBB" }, { cat: 1, id: "CCCC" }, { cat: 1, id: "DDDD" }, { cat: 1, id: "EEEE" }, { cat: 1, id: "FFFF" }, { cat: 1, id: "GGGG" }, { cat: 2, id: "HHHH" }, { cat: 2, id: "IIII" }, { cat: 2, id: "JJJJ" }, { cat: 3, id: "KKKK" }],
            result = sortDates(data);

            console.log(result);

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





            function sortDates() {
            var i, sort = ;
            for (i = 0; i < data.length; i++) {
            sort[data[i].cat] = sort[data[i].cat] || ;
            sort[data[i].cat].push(data[i].id);
            }
            return sort;
            }

            var data = [{ cat: 0, id: "AAAA" }, { cat: 0, id: "BBBB" }, { cat: 1, id: "CCCC" }, { cat: 1, id: "DDDD" }, { cat: 1, id: "EEEE" }, { cat: 1, id: "FFFF" }, { cat: 1, id: "GGGG" }, { cat: 2, id: "HHHH" }, { cat: 2, id: "IIII" }, { cat: 2, id: "JJJJ" }, { cat: 3, id: "KKKK" }],
            result = sortDates(data);

            console.log(result);

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






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 25 '18 at 19:28

























            answered Nov 25 '18 at 19:23









            Nina ScholzNina Scholz

            191k15104176




            191k15104176













            • Absolutely perfect - thank you! I knew there was a more efficient way to do this... so the line: sort[data[i].cat] = sort[data[i].cat] || ; Is this basically saying, push data[i] to sort[data[i].cat], or if sort[data[i].cat doesn't exist, use the logic OR, which pushes it into a new item in sort?

              – default-LA
              Nov 25 '18 at 20:38











            • right first the check and if not a truthy value, like an array, then assign an array and later push the new value.

              – Nina Scholz
              Nov 25 '18 at 20:41



















            • Absolutely perfect - thank you! I knew there was a more efficient way to do this... so the line: sort[data[i].cat] = sort[data[i].cat] || ; Is this basically saying, push data[i] to sort[data[i].cat], or if sort[data[i].cat doesn't exist, use the logic OR, which pushes it into a new item in sort?

              – default-LA
              Nov 25 '18 at 20:38











            • right first the check and if not a truthy value, like an array, then assign an array and later push the new value.

              – Nina Scholz
              Nov 25 '18 at 20:41

















            Absolutely perfect - thank you! I knew there was a more efficient way to do this... so the line: sort[data[i].cat] = sort[data[i].cat] || ; Is this basically saying, push data[i] to sort[data[i].cat], or if sort[data[i].cat doesn't exist, use the logic OR, which pushes it into a new item in sort?

            – default-LA
            Nov 25 '18 at 20:38





            Absolutely perfect - thank you! I knew there was a more efficient way to do this... so the line: sort[data[i].cat] = sort[data[i].cat] || ; Is this basically saying, push data[i] to sort[data[i].cat], or if sort[data[i].cat doesn't exist, use the logic OR, which pushes it into a new item in sort?

            – default-LA
            Nov 25 '18 at 20:38













            right first the check and if not a truthy value, like an array, then assign an array and later push the new value.

            – Nina Scholz
            Nov 25 '18 at 20:41





            right first the check and if not a truthy value, like an array, then assign an array and later push the new value.

            – Nina Scholz
            Nov 25 '18 at 20:41













            0














            Another way you can achieve this with ES6 in a more concise manner is to use Array.reduce and inside Array.sort via String.localeCompare:






            var data = [{ "cat": 0, "id": "BBBB" }, { "cat": 0, "id": "AAAA" }, { "cat": 1, "id": "CCCC" }, { "cat": 1, "id": "DDDD" }, { "cat": 1, "id": "EEEE" }, { "cat": 1, "id": "FFFF" }, { "cat": 1, "id": "GGGG" }, { "cat": 2, "id": "HHHH" }, { "cat": 2, "id": "IIII" }, { "cat": 2, "id": "JJJJ" }, { "cat": 3, "id": "KKKK" } ]

            const result = Object.values(data.reduce((r,c) => {
            r[c.cat] = [...r[c.cat] || , c.id].sort((a,b) => a.localeCompare(b))
            return r
            }, {}))
            console.log(result)





            String.localeCompare has various options in terms of how to compare (like dealing with numbers in the strings, case sensitivity etc)



            The idea is to use the Array.reduce is to group the entries in an object and then just use Object.values to get the desired output (milti-dimentional array).






            share|improve this answer




























              0














              Another way you can achieve this with ES6 in a more concise manner is to use Array.reduce and inside Array.sort via String.localeCompare:






              var data = [{ "cat": 0, "id": "BBBB" }, { "cat": 0, "id": "AAAA" }, { "cat": 1, "id": "CCCC" }, { "cat": 1, "id": "DDDD" }, { "cat": 1, "id": "EEEE" }, { "cat": 1, "id": "FFFF" }, { "cat": 1, "id": "GGGG" }, { "cat": 2, "id": "HHHH" }, { "cat": 2, "id": "IIII" }, { "cat": 2, "id": "JJJJ" }, { "cat": 3, "id": "KKKK" } ]

              const result = Object.values(data.reduce((r,c) => {
              r[c.cat] = [...r[c.cat] || , c.id].sort((a,b) => a.localeCompare(b))
              return r
              }, {}))
              console.log(result)





              String.localeCompare has various options in terms of how to compare (like dealing with numbers in the strings, case sensitivity etc)



              The idea is to use the Array.reduce is to group the entries in an object and then just use Object.values to get the desired output (milti-dimentional array).






              share|improve this answer


























                0












                0








                0







                Another way you can achieve this with ES6 in a more concise manner is to use Array.reduce and inside Array.sort via String.localeCompare:






                var data = [{ "cat": 0, "id": "BBBB" }, { "cat": 0, "id": "AAAA" }, { "cat": 1, "id": "CCCC" }, { "cat": 1, "id": "DDDD" }, { "cat": 1, "id": "EEEE" }, { "cat": 1, "id": "FFFF" }, { "cat": 1, "id": "GGGG" }, { "cat": 2, "id": "HHHH" }, { "cat": 2, "id": "IIII" }, { "cat": 2, "id": "JJJJ" }, { "cat": 3, "id": "KKKK" } ]

                const result = Object.values(data.reduce((r,c) => {
                r[c.cat] = [...r[c.cat] || , c.id].sort((a,b) => a.localeCompare(b))
                return r
                }, {}))
                console.log(result)





                String.localeCompare has various options in terms of how to compare (like dealing with numbers in the strings, case sensitivity etc)



                The idea is to use the Array.reduce is to group the entries in an object and then just use Object.values to get the desired output (milti-dimentional array).






                share|improve this answer













                Another way you can achieve this with ES6 in a more concise manner is to use Array.reduce and inside Array.sort via String.localeCompare:






                var data = [{ "cat": 0, "id": "BBBB" }, { "cat": 0, "id": "AAAA" }, { "cat": 1, "id": "CCCC" }, { "cat": 1, "id": "DDDD" }, { "cat": 1, "id": "EEEE" }, { "cat": 1, "id": "FFFF" }, { "cat": 1, "id": "GGGG" }, { "cat": 2, "id": "HHHH" }, { "cat": 2, "id": "IIII" }, { "cat": 2, "id": "JJJJ" }, { "cat": 3, "id": "KKKK" } ]

                const result = Object.values(data.reduce((r,c) => {
                r[c.cat] = [...r[c.cat] || , c.id].sort((a,b) => a.localeCompare(b))
                return r
                }, {}))
                console.log(result)





                String.localeCompare has various options in terms of how to compare (like dealing with numbers in the strings, case sensitivity etc)



                The idea is to use the Array.reduce is to group the entries in an object and then just use Object.values to get the desired output (milti-dimentional array).






                var data = [{ "cat": 0, "id": "BBBB" }, { "cat": 0, "id": "AAAA" }, { "cat": 1, "id": "CCCC" }, { "cat": 1, "id": "DDDD" }, { "cat": 1, "id": "EEEE" }, { "cat": 1, "id": "FFFF" }, { "cat": 1, "id": "GGGG" }, { "cat": 2, "id": "HHHH" }, { "cat": 2, "id": "IIII" }, { "cat": 2, "id": "JJJJ" }, { "cat": 3, "id": "KKKK" } ]

                const result = Object.values(data.reduce((r,c) => {
                r[c.cat] = [...r[c.cat] || , c.id].sort((a,b) => a.localeCompare(b))
                return r
                }, {}))
                console.log(result)





                var data = [{ "cat": 0, "id": "BBBB" }, { "cat": 0, "id": "AAAA" }, { "cat": 1, "id": "CCCC" }, { "cat": 1, "id": "DDDD" }, { "cat": 1, "id": "EEEE" }, { "cat": 1, "id": "FFFF" }, { "cat": 1, "id": "GGGG" }, { "cat": 2, "id": "HHHH" }, { "cat": 2, "id": "IIII" }, { "cat": 2, "id": "JJJJ" }, { "cat": 3, "id": "KKKK" } ]

                const result = Object.values(data.reduce((r,c) => {
                r[c.cat] = [...r[c.cat] || , c.id].sort((a,b) => a.localeCompare(b))
                return r
                }, {}))
                console.log(result)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 '18 at 20:46









                AkrionAkrion

                9,53011224




                9,53011224






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471015%2fmultidimensional-array-of-an-unknown-size-in-javascript%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Create new schema in PostgreSQL using DBeaver

                    Deepest pit of an array with Javascript: test on Codility

                    Costa Masnaga