Filter array of objects based on a field in each object





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















Let's say I have an array of objects like this:



[
{ "type": 121, "model": "model1" },
{ "type": 128, "model": "model2" },
{ "type": 130, "model": "model2" },
{ "type": 130, "model": "model2" }
]


And I want to filter through it and create new objects based on the model field.



With the end result being:



[
{ "type": 121, "model": "model1" }
]

[
{ "type": 128, "model": "model2" },
{ "type": 130, "model": "model2" },
{ "type": 130, "model": "model2" }
]


I'm using typescript and lodash so anything with that would be best



I tried lodash groupBy and ES6 mapping, but no success so far. I guess I could do it in a dirty way with multiple forEach loops but I'm pretty sure there is a much easier way.










share|improve this question























  • can you please provide your code or https://stackblitz.com/?

    – Abhishek
    Jan 4 at 10:39


















1















Let's say I have an array of objects like this:



[
{ "type": 121, "model": "model1" },
{ "type": 128, "model": "model2" },
{ "type": 130, "model": "model2" },
{ "type": 130, "model": "model2" }
]


And I want to filter through it and create new objects based on the model field.



With the end result being:



[
{ "type": 121, "model": "model1" }
]

[
{ "type": 128, "model": "model2" },
{ "type": 130, "model": "model2" },
{ "type": 130, "model": "model2" }
]


I'm using typescript and lodash so anything with that would be best



I tried lodash groupBy and ES6 mapping, but no success so far. I guess I could do it in a dirty way with multiple forEach loops but I'm pretty sure there is a much easier way.










share|improve this question























  • can you please provide your code or https://stackblitz.com/?

    – Abhishek
    Jan 4 at 10:39














1












1








1








Let's say I have an array of objects like this:



[
{ "type": 121, "model": "model1" },
{ "type": 128, "model": "model2" },
{ "type": 130, "model": "model2" },
{ "type": 130, "model": "model2" }
]


And I want to filter through it and create new objects based on the model field.



With the end result being:



[
{ "type": 121, "model": "model1" }
]

[
{ "type": 128, "model": "model2" },
{ "type": 130, "model": "model2" },
{ "type": 130, "model": "model2" }
]


I'm using typescript and lodash so anything with that would be best



I tried lodash groupBy and ES6 mapping, but no success so far. I guess I could do it in a dirty way with multiple forEach loops but I'm pretty sure there is a much easier way.










share|improve this question














Let's say I have an array of objects like this:



[
{ "type": 121, "model": "model1" },
{ "type": 128, "model": "model2" },
{ "type": 130, "model": "model2" },
{ "type": 130, "model": "model2" }
]


And I want to filter through it and create new objects based on the model field.



With the end result being:



[
{ "type": 121, "model": "model1" }
]

[
{ "type": 128, "model": "model2" },
{ "type": 130, "model": "model2" },
{ "type": 130, "model": "model2" }
]


I'm using typescript and lodash so anything with that would be best



I tried lodash groupBy and ES6 mapping, but no success so far. I guess I could do it in a dirty way with multiple forEach loops but I'm pretty sure there is a much easier way.







angular typescript object ecmascript-6 lodash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 4 at 10:34









rauliciousraulicious

991213




991213













  • can you please provide your code or https://stackblitz.com/?

    – Abhishek
    Jan 4 at 10:39



















  • can you please provide your code or https://stackblitz.com/?

    – Abhishek
    Jan 4 at 10:39

















can you please provide your code or https://stackblitz.com/?

– Abhishek
Jan 4 at 10:39





can you please provide your code or https://stackblitz.com/?

– Abhishek
Jan 4 at 10:39












6 Answers
6






active

oldest

votes


















0














See, if this help with lodash






var data = [
{ "type": 121, "model": "model1" },
{ "type": 128, "model": "model2" },
{ "type": 130, "model": "model2" },
{ "type": 130, "model": "model2" }
];

var grouped = _.groupBy(data, function(item) {
return item.model;
});

console.log(grouped);

<script src='https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js'></script>








share|improve this answer































    2














    You can use .filter, like this:



    let newarray1 = array.filter(obj => obj.model === 'model1');
    let newarray2 = array.filter(obj => obj.model === 'model2');
    ...





    share|improve this answer































      1














      You can simply user Array.filter function to filter out array to new array object. Here I am using variable searchModels which will have list of models which you want to filter and inside filter I am checking condition searchModels.indexOf(item.model) to check model value.






      var array = [
      { "type": 121, "model": "model1" },
      { "type": 128, "model": "model2" },
      { "type": 130, "model": "model2" },
      { "type": 130, "model": "model2" }
      ];


      var searchModels = ['model1', 'model3'];
      var filteredArray = array.filter((item) => { return searchModels.indexOf(item.model) !== -1 });
      console.log("Original: " , array);
      console.log("Filtered: " , filteredArray);








      share|improve this answer































        1














        You could so something like that for example :)
        In this case I am creating a Map, I think it's easier for search



        var newData = new Map();

        var originalData = [{"type": 130, "model": "model1"}, {"type": 130, "model": "model2"}];
        var formattedData = originalData.map(obj => {
        newData[obj.model].push(obj.type);
        });


        You will get something like :



        newData = ([
        [ "model1", "130" ],
        [ "model1", "128" ],
        [ "model2", "3" ]
        ]);


        And you can get any values using the model key newData["model2"] = ["3"]






        share|improve this answer































          1














          you can use Array.prototype.reduce method to group all.






          let array = [
          { "type": 121, "model": "model1" },
          { "type": 128, "model": "model2" },
          { "type": 130, "model": "model2" },
          { "type": 130, "model": "model2" }
          ]

          let all = [...array.reduce((acc, curr) => {
          acc.has(curr.model) ? acc.set(curr.model, [...acc.get(curr.model), curr]): acc.set(curr.model, [curr]);
          return acc;
          }, new Map()).values()];
          console.log(...all)








          share|improve this answer































            1














            try this @raulicious,






            var arrayList= [
            { "type": 121, "model": "model1" },
            { "type": 128, "model": "model2" },
            { "type": 130, "model": "model2" },
            { "type": 131, "model": "model2" }
            ];


            var filteredArray = ;
            var filtered = ;

            arrayList.sort((a, b) => {
            if(a.model == b.model) {
            filtered.push(b);
            } else {
            filtered.push(b);
            filteredArray.push(filtered);
            filtered = ;
            }
            filtered.push(a);
            filteredArray.push(filtered);
            });
            console.log(filteredArray);





            I know this has some redundant code, I am trying to reduce that soon






            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%2f54037248%2ffilter-array-of-objects-based-on-a-field-in-each-object%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              6 Answers
              6






              active

              oldest

              votes








              6 Answers
              6






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              See, if this help with lodash






              var data = [
              { "type": 121, "model": "model1" },
              { "type": 128, "model": "model2" },
              { "type": 130, "model": "model2" },
              { "type": 130, "model": "model2" }
              ];

              var grouped = _.groupBy(data, function(item) {
              return item.model;
              });

              console.log(grouped);

              <script src='https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js'></script>








              share|improve this answer




























                0














                See, if this help with lodash






                var data = [
                { "type": 121, "model": "model1" },
                { "type": 128, "model": "model2" },
                { "type": 130, "model": "model2" },
                { "type": 130, "model": "model2" }
                ];

                var grouped = _.groupBy(data, function(item) {
                return item.model;
                });

                console.log(grouped);

                <script src='https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js'></script>








                share|improve this answer


























                  0












                  0








                  0







                  See, if this help with lodash






                  var data = [
                  { "type": 121, "model": "model1" },
                  { "type": 128, "model": "model2" },
                  { "type": 130, "model": "model2" },
                  { "type": 130, "model": "model2" }
                  ];

                  var grouped = _.groupBy(data, function(item) {
                  return item.model;
                  });

                  console.log(grouped);

                  <script src='https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js'></script>








                  share|improve this answer













                  See, if this help with lodash






                  var data = [
                  { "type": 121, "model": "model1" },
                  { "type": 128, "model": "model2" },
                  { "type": 130, "model": "model2" },
                  { "type": 130, "model": "model2" }
                  ];

                  var grouped = _.groupBy(data, function(item) {
                  return item.model;
                  });

                  console.log(grouped);

                  <script src='https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js'></script>








                  var data = [
                  { "type": 121, "model": "model1" },
                  { "type": 128, "model": "model2" },
                  { "type": 130, "model": "model2" },
                  { "type": 130, "model": "model2" }
                  ];

                  var grouped = _.groupBy(data, function(item) {
                  return item.model;
                  });

                  console.log(grouped);

                  <script src='https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js'></script>





                  var data = [
                  { "type": 121, "model": "model1" },
                  { "type": 128, "model": "model2" },
                  { "type": 130, "model": "model2" },
                  { "type": 130, "model": "model2" }
                  ];

                  var grouped = _.groupBy(data, function(item) {
                  return item.model;
                  });

                  console.log(grouped);

                  <script src='https://cdn.jsdelivr.net/lodash/4.17.2/lodash.min.js'></script>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 4 at 11:15









                  Rohit.007Rohit.007

                  1,7892521




                  1,7892521

























                      2














                      You can use .filter, like this:



                      let newarray1 = array.filter(obj => obj.model === 'model1');
                      let newarray2 = array.filter(obj => obj.model === 'model2');
                      ...





                      share|improve this answer




























                        2














                        You can use .filter, like this:



                        let newarray1 = array.filter(obj => obj.model === 'model1');
                        let newarray2 = array.filter(obj => obj.model === 'model2');
                        ...





                        share|improve this answer


























                          2












                          2








                          2







                          You can use .filter, like this:



                          let newarray1 = array.filter(obj => obj.model === 'model1');
                          let newarray2 = array.filter(obj => obj.model === 'model2');
                          ...





                          share|improve this answer













                          You can use .filter, like this:



                          let newarray1 = array.filter(obj => obj.model === 'model1');
                          let newarray2 = array.filter(obj => obj.model === 'model2');
                          ...






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 4 at 10:40









                          Pedro LimaPedro Lima

                          432411




                          432411























                              1














                              You can simply user Array.filter function to filter out array to new array object. Here I am using variable searchModels which will have list of models which you want to filter and inside filter I am checking condition searchModels.indexOf(item.model) to check model value.






                              var array = [
                              { "type": 121, "model": "model1" },
                              { "type": 128, "model": "model2" },
                              { "type": 130, "model": "model2" },
                              { "type": 130, "model": "model2" }
                              ];


                              var searchModels = ['model1', 'model3'];
                              var filteredArray = array.filter((item) => { return searchModels.indexOf(item.model) !== -1 });
                              console.log("Original: " , array);
                              console.log("Filtered: " , filteredArray);








                              share|improve this answer




























                                1














                                You can simply user Array.filter function to filter out array to new array object. Here I am using variable searchModels which will have list of models which you want to filter and inside filter I am checking condition searchModels.indexOf(item.model) to check model value.






                                var array = [
                                { "type": 121, "model": "model1" },
                                { "type": 128, "model": "model2" },
                                { "type": 130, "model": "model2" },
                                { "type": 130, "model": "model2" }
                                ];


                                var searchModels = ['model1', 'model3'];
                                var filteredArray = array.filter((item) => { return searchModels.indexOf(item.model) !== -1 });
                                console.log("Original: " , array);
                                console.log("Filtered: " , filteredArray);








                                share|improve this answer


























                                  1












                                  1








                                  1







                                  You can simply user Array.filter function to filter out array to new array object. Here I am using variable searchModels which will have list of models which you want to filter and inside filter I am checking condition searchModels.indexOf(item.model) to check model value.






                                  var array = [
                                  { "type": 121, "model": "model1" },
                                  { "type": 128, "model": "model2" },
                                  { "type": 130, "model": "model2" },
                                  { "type": 130, "model": "model2" }
                                  ];


                                  var searchModels = ['model1', 'model3'];
                                  var filteredArray = array.filter((item) => { return searchModels.indexOf(item.model) !== -1 });
                                  console.log("Original: " , array);
                                  console.log("Filtered: " , filteredArray);








                                  share|improve this answer













                                  You can simply user Array.filter function to filter out array to new array object. Here I am using variable searchModels which will have list of models which you want to filter and inside filter I am checking condition searchModels.indexOf(item.model) to check model value.






                                  var array = [
                                  { "type": 121, "model": "model1" },
                                  { "type": 128, "model": "model2" },
                                  { "type": 130, "model": "model2" },
                                  { "type": 130, "model": "model2" }
                                  ];


                                  var searchModels = ['model1', 'model3'];
                                  var filteredArray = array.filter((item) => { return searchModels.indexOf(item.model) !== -1 });
                                  console.log("Original: " , array);
                                  console.log("Filtered: " , filteredArray);








                                  var array = [
                                  { "type": 121, "model": "model1" },
                                  { "type": 128, "model": "model2" },
                                  { "type": 130, "model": "model2" },
                                  { "type": 130, "model": "model2" }
                                  ];


                                  var searchModels = ['model1', 'model3'];
                                  var filteredArray = array.filter((item) => { return searchModels.indexOf(item.model) !== -1 });
                                  console.log("Original: " , array);
                                  console.log("Filtered: " , filteredArray);





                                  var array = [
                                  { "type": 121, "model": "model1" },
                                  { "type": 128, "model": "model2" },
                                  { "type": 130, "model": "model2" },
                                  { "type": 130, "model": "model2" }
                                  ];


                                  var searchModels = ['model1', 'model3'];
                                  var filteredArray = array.filter((item) => { return searchModels.indexOf(item.model) !== -1 });
                                  console.log("Original: " , array);
                                  console.log("Filtered: " , filteredArray);






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Jan 4 at 10:42









                                  sriharsha_bhatsriharsha_bhat

                                  529311




                                  529311























                                      1














                                      You could so something like that for example :)
                                      In this case I am creating a Map, I think it's easier for search



                                      var newData = new Map();

                                      var originalData = [{"type": 130, "model": "model1"}, {"type": 130, "model": "model2"}];
                                      var formattedData = originalData.map(obj => {
                                      newData[obj.model].push(obj.type);
                                      });


                                      You will get something like :



                                      newData = ([
                                      [ "model1", "130" ],
                                      [ "model1", "128" ],
                                      [ "model2", "3" ]
                                      ]);


                                      And you can get any values using the model key newData["model2"] = ["3"]






                                      share|improve this answer




























                                        1














                                        You could so something like that for example :)
                                        In this case I am creating a Map, I think it's easier for search



                                        var newData = new Map();

                                        var originalData = [{"type": 130, "model": "model1"}, {"type": 130, "model": "model2"}];
                                        var formattedData = originalData.map(obj => {
                                        newData[obj.model].push(obj.type);
                                        });


                                        You will get something like :



                                        newData = ([
                                        [ "model1", "130" ],
                                        [ "model1", "128" ],
                                        [ "model2", "3" ]
                                        ]);


                                        And you can get any values using the model key newData["model2"] = ["3"]






                                        share|improve this answer


























                                          1












                                          1








                                          1







                                          You could so something like that for example :)
                                          In this case I am creating a Map, I think it's easier for search



                                          var newData = new Map();

                                          var originalData = [{"type": 130, "model": "model1"}, {"type": 130, "model": "model2"}];
                                          var formattedData = originalData.map(obj => {
                                          newData[obj.model].push(obj.type);
                                          });


                                          You will get something like :



                                          newData = ([
                                          [ "model1", "130" ],
                                          [ "model1", "128" ],
                                          [ "model2", "3" ]
                                          ]);


                                          And you can get any values using the model key newData["model2"] = ["3"]






                                          share|improve this answer













                                          You could so something like that for example :)
                                          In this case I am creating a Map, I think it's easier for search



                                          var newData = new Map();

                                          var originalData = [{"type": 130, "model": "model1"}, {"type": 130, "model": "model2"}];
                                          var formattedData = originalData.map(obj => {
                                          newData[obj.model].push(obj.type);
                                          });


                                          You will get something like :



                                          newData = ([
                                          [ "model1", "130" ],
                                          [ "model1", "128" ],
                                          [ "model2", "3" ]
                                          ]);


                                          And you can get any values using the model key newData["model2"] = ["3"]







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Jan 4 at 10:47









                                          andrea06590andrea06590

                                          682313




                                          682313























                                              1














                                              you can use Array.prototype.reduce method to group all.






                                              let array = [
                                              { "type": 121, "model": "model1" },
                                              { "type": 128, "model": "model2" },
                                              { "type": 130, "model": "model2" },
                                              { "type": 130, "model": "model2" }
                                              ]

                                              let all = [...array.reduce((acc, curr) => {
                                              acc.has(curr.model) ? acc.set(curr.model, [...acc.get(curr.model), curr]): acc.set(curr.model, [curr]);
                                              return acc;
                                              }, new Map()).values()];
                                              console.log(...all)








                                              share|improve this answer




























                                                1














                                                you can use Array.prototype.reduce method to group all.






                                                let array = [
                                                { "type": 121, "model": "model1" },
                                                { "type": 128, "model": "model2" },
                                                { "type": 130, "model": "model2" },
                                                { "type": 130, "model": "model2" }
                                                ]

                                                let all = [...array.reduce((acc, curr) => {
                                                acc.has(curr.model) ? acc.set(curr.model, [...acc.get(curr.model), curr]): acc.set(curr.model, [curr]);
                                                return acc;
                                                }, new Map()).values()];
                                                console.log(...all)








                                                share|improve this answer


























                                                  1












                                                  1








                                                  1







                                                  you can use Array.prototype.reduce method to group all.






                                                  let array = [
                                                  { "type": 121, "model": "model1" },
                                                  { "type": 128, "model": "model2" },
                                                  { "type": 130, "model": "model2" },
                                                  { "type": 130, "model": "model2" }
                                                  ]

                                                  let all = [...array.reduce((acc, curr) => {
                                                  acc.has(curr.model) ? acc.set(curr.model, [...acc.get(curr.model), curr]): acc.set(curr.model, [curr]);
                                                  return acc;
                                                  }, new Map()).values()];
                                                  console.log(...all)








                                                  share|improve this answer













                                                  you can use Array.prototype.reduce method to group all.






                                                  let array = [
                                                  { "type": 121, "model": "model1" },
                                                  { "type": 128, "model": "model2" },
                                                  { "type": 130, "model": "model2" },
                                                  { "type": 130, "model": "model2" }
                                                  ]

                                                  let all = [...array.reduce((acc, curr) => {
                                                  acc.has(curr.model) ? acc.set(curr.model, [...acc.get(curr.model), curr]): acc.set(curr.model, [curr]);
                                                  return acc;
                                                  }, new Map()).values()];
                                                  console.log(...all)








                                                  let array = [
                                                  { "type": 121, "model": "model1" },
                                                  { "type": 128, "model": "model2" },
                                                  { "type": 130, "model": "model2" },
                                                  { "type": 130, "model": "model2" }
                                                  ]

                                                  let all = [...array.reduce((acc, curr) => {
                                                  acc.has(curr.model) ? acc.set(curr.model, [...acc.get(curr.model), curr]): acc.set(curr.model, [curr]);
                                                  return acc;
                                                  }, new Map()).values()];
                                                  console.log(...all)





                                                  let array = [
                                                  { "type": 121, "model": "model1" },
                                                  { "type": 128, "model": "model2" },
                                                  { "type": 130, "model": "model2" },
                                                  { "type": 130, "model": "model2" }
                                                  ]

                                                  let all = [...array.reduce((acc, curr) => {
                                                  acc.has(curr.model) ? acc.set(curr.model, [...acc.get(curr.model), curr]): acc.set(curr.model, [curr]);
                                                  return acc;
                                                  }, new Map()).values()];
                                                  console.log(...all)






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Jan 4 at 11:21









                                                  AZ_AZ_

                                                  917310




                                                  917310























                                                      1














                                                      try this @raulicious,






                                                      var arrayList= [
                                                      { "type": 121, "model": "model1" },
                                                      { "type": 128, "model": "model2" },
                                                      { "type": 130, "model": "model2" },
                                                      { "type": 131, "model": "model2" }
                                                      ];


                                                      var filteredArray = ;
                                                      var filtered = ;

                                                      arrayList.sort((a, b) => {
                                                      if(a.model == b.model) {
                                                      filtered.push(b);
                                                      } else {
                                                      filtered.push(b);
                                                      filteredArray.push(filtered);
                                                      filtered = ;
                                                      }
                                                      filtered.push(a);
                                                      filteredArray.push(filtered);
                                                      });
                                                      console.log(filteredArray);





                                                      I know this has some redundant code, I am trying to reduce that soon






                                                      share|improve this answer






























                                                        1














                                                        try this @raulicious,






                                                        var arrayList= [
                                                        { "type": 121, "model": "model1" },
                                                        { "type": 128, "model": "model2" },
                                                        { "type": 130, "model": "model2" },
                                                        { "type": 131, "model": "model2" }
                                                        ];


                                                        var filteredArray = ;
                                                        var filtered = ;

                                                        arrayList.sort((a, b) => {
                                                        if(a.model == b.model) {
                                                        filtered.push(b);
                                                        } else {
                                                        filtered.push(b);
                                                        filteredArray.push(filtered);
                                                        filtered = ;
                                                        }
                                                        filtered.push(a);
                                                        filteredArray.push(filtered);
                                                        });
                                                        console.log(filteredArray);





                                                        I know this has some redundant code, I am trying to reduce that soon






                                                        share|improve this answer




























                                                          1












                                                          1








                                                          1







                                                          try this @raulicious,






                                                          var arrayList= [
                                                          { "type": 121, "model": "model1" },
                                                          { "type": 128, "model": "model2" },
                                                          { "type": 130, "model": "model2" },
                                                          { "type": 131, "model": "model2" }
                                                          ];


                                                          var filteredArray = ;
                                                          var filtered = ;

                                                          arrayList.sort((a, b) => {
                                                          if(a.model == b.model) {
                                                          filtered.push(b);
                                                          } else {
                                                          filtered.push(b);
                                                          filteredArray.push(filtered);
                                                          filtered = ;
                                                          }
                                                          filtered.push(a);
                                                          filteredArray.push(filtered);
                                                          });
                                                          console.log(filteredArray);





                                                          I know this has some redundant code, I am trying to reduce that soon






                                                          share|improve this answer















                                                          try this @raulicious,






                                                          var arrayList= [
                                                          { "type": 121, "model": "model1" },
                                                          { "type": 128, "model": "model2" },
                                                          { "type": 130, "model": "model2" },
                                                          { "type": 131, "model": "model2" }
                                                          ];


                                                          var filteredArray = ;
                                                          var filtered = ;

                                                          arrayList.sort((a, b) => {
                                                          if(a.model == b.model) {
                                                          filtered.push(b);
                                                          } else {
                                                          filtered.push(b);
                                                          filteredArray.push(filtered);
                                                          filtered = ;
                                                          }
                                                          filtered.push(a);
                                                          filteredArray.push(filtered);
                                                          });
                                                          console.log(filteredArray);





                                                          I know this has some redundant code, I am trying to reduce that soon






                                                          var arrayList= [
                                                          { "type": 121, "model": "model1" },
                                                          { "type": 128, "model": "model2" },
                                                          { "type": 130, "model": "model2" },
                                                          { "type": 131, "model": "model2" }
                                                          ];


                                                          var filteredArray = ;
                                                          var filtered = ;

                                                          arrayList.sort((a, b) => {
                                                          if(a.model == b.model) {
                                                          filtered.push(b);
                                                          } else {
                                                          filtered.push(b);
                                                          filteredArray.push(filtered);
                                                          filtered = ;
                                                          }
                                                          filtered.push(a);
                                                          filteredArray.push(filtered);
                                                          });
                                                          console.log(filteredArray);





                                                          var arrayList= [
                                                          { "type": 121, "model": "model1" },
                                                          { "type": 128, "model": "model2" },
                                                          { "type": 130, "model": "model2" },
                                                          { "type": 131, "model": "model2" }
                                                          ];


                                                          var filteredArray = ;
                                                          var filtered = ;

                                                          arrayList.sort((a, b) => {
                                                          if(a.model == b.model) {
                                                          filtered.push(b);
                                                          } else {
                                                          filtered.push(b);
                                                          filteredArray.push(filtered);
                                                          filtered = ;
                                                          }
                                                          filtered.push(a);
                                                          filteredArray.push(filtered);
                                                          });
                                                          console.log(filteredArray);






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited Jan 4 at 12:05

























                                                          answered Jan 4 at 11:22









                                                          ganesh045ganesh045

                                                          1,340515




                                                          1,340515






























                                                              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%2f54037248%2ffilter-array-of-objects-based-on-a-field-in-each-object%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

                                                              Monofisismo

                                                              Angular Downloading a file using contenturl with Basic Authentication

                                                              Olmecas