Filtering array so it would remove all undefined objects












0














I need to filter an array so it would remove undefined objects from it.



enter image description here



I tried with lodash _.filter but didn't succeed (returned completely empty array)



_.filter(myArray, _.isEmpty)


I'm using Angular 6 so anything with typescript or lodash would be perfect.










share|improve this question



























    0














    I need to filter an array so it would remove undefined objects from it.



    enter image description here



    I tried with lodash _.filter but didn't succeed (returned completely empty array)



    _.filter(myArray, _.isEmpty)


    I'm using Angular 6 so anything with typescript or lodash would be perfect.










    share|improve this question

























      0












      0








      0







      I need to filter an array so it would remove undefined objects from it.



      enter image description here



      I tried with lodash _.filter but didn't succeed (returned completely empty array)



      _.filter(myArray, _.isEmpty)


      I'm using Angular 6 so anything with typescript or lodash would be perfect.










      share|improve this question













      I need to filter an array so it would remove undefined objects from it.



      enter image description here



      I tried with lodash _.filter but didn't succeed (returned completely empty array)



      _.filter(myArray, _.isEmpty)


      I'm using Angular 6 so anything with typescript or lodash would be perfect.







      arrays angular object lodash






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 9:01









      raulicious

      941212




      941212
























          6 Answers
          6






          active

          oldest

          votes


















          1














          An easier way:



          _.filter(myArray, Boolean)


          This rids the array of nulls, 0's and undefined's.






          share|improve this answer





















          • Thanks a lot, since this is the cleanest way I will accept this answer once I can.
            – raulicious
            Nov 12 at 9:08



















          1














          The more simple way



          _.filter(myArray, function(o) { return o !== undefined });





          share|improve this answer































            1














            Using Javascript also feasible. it supports null,undefined, 0, empty.



            newArray = myArray.filter(item=> item);





            share|improve this answer





























              1














              You don't need a library; the Javascript array type has a filter method:



              var filteredArray = myArray.filter(item => item !== undefined);





              share|improve this answer





























                1














                In filter function of lodash if the callback (you passed as argument) return truthy value that element considered to be retained in resultant array otherwise (in case return falsy) it will not retain that element. Your isEmpty return true if it is Empty and thus the result retains those values (null, undefined, 0, ...). So you can either use



                _.filter(myArray, _.negate(_.IsEmpty)) or _.filter(myArray, v => !_.IsEmpty(v))



                In the way you are trying



                Or, you can directly use _.filter(myArray) but in this case it will not remove empty object or empty array as same like _.filter(myArray, Boolean), passing Boolean is not necessery in case of using lodash



                in case you don't want to negate and want a simpler solution for removing all the empty element, then you can use



                _.reject(myArray, _.isEmpty)






                share|improve this answer































                  0














                  var newArray = array.filter(arrayItem => arrayItem)


                  It will Filtering array so it would remove all undefined objects and assign to new variable called newArray






                  share|improve this answer























                  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
                    – Nick
                    Nov 12 at 10:25











                  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%2f53258766%2ffiltering-array-so-it-would-remove-all-undefined-objects%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









                  1














                  An easier way:



                  _.filter(myArray, Boolean)


                  This rids the array of nulls, 0's and undefined's.






                  share|improve this answer





















                  • Thanks a lot, since this is the cleanest way I will accept this answer once I can.
                    – raulicious
                    Nov 12 at 9:08
















                  1














                  An easier way:



                  _.filter(myArray, Boolean)


                  This rids the array of nulls, 0's and undefined's.






                  share|improve this answer





















                  • Thanks a lot, since this is the cleanest way I will accept this answer once I can.
                    – raulicious
                    Nov 12 at 9:08














                  1












                  1








                  1






                  An easier way:



                  _.filter(myArray, Boolean)


                  This rids the array of nulls, 0's and undefined's.






                  share|improve this answer












                  An easier way:



                  _.filter(myArray, Boolean)


                  This rids the array of nulls, 0's and undefined's.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 9:04









                  rrd

                  2,83831222




                  2,83831222












                  • Thanks a lot, since this is the cleanest way I will accept this answer once I can.
                    – raulicious
                    Nov 12 at 9:08


















                  • Thanks a lot, since this is the cleanest way I will accept this answer once I can.
                    – raulicious
                    Nov 12 at 9:08
















                  Thanks a lot, since this is the cleanest way I will accept this answer once I can.
                  – raulicious
                  Nov 12 at 9:08




                  Thanks a lot, since this is the cleanest way I will accept this answer once I can.
                  – raulicious
                  Nov 12 at 9:08













                  1














                  The more simple way



                  _.filter(myArray, function(o) { return o !== undefined });





                  share|improve this answer




























                    1














                    The more simple way



                    _.filter(myArray, function(o) { return o !== undefined });





                    share|improve this answer


























                      1












                      1








                      1






                      The more simple way



                      _.filter(myArray, function(o) { return o !== undefined });





                      share|improve this answer














                      The more simple way



                      _.filter(myArray, function(o) { return o !== undefined });






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 12 at 9:04









                      Ayush Gupta

                      3,0261643




                      3,0261643










                      answered Nov 12 at 9:03









                      firegloves

                      3,21511025




                      3,21511025























                          1














                          Using Javascript also feasible. it supports null,undefined, 0, empty.



                          newArray = myArray.filter(item=> item);





                          share|improve this answer


























                            1














                            Using Javascript also feasible. it supports null,undefined, 0, empty.



                            newArray = myArray.filter(item=> item);





                            share|improve this answer
























                              1












                              1








                              1






                              Using Javascript also feasible. it supports null,undefined, 0, empty.



                              newArray = myArray.filter(item=> item);





                              share|improve this answer












                              Using Javascript also feasible. it supports null,undefined, 0, empty.



                              newArray = myArray.filter(item=> item);






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 12 at 9:05









                              Suresh Kumar Ariya

                              4,4131215




                              4,4131215























                                  1














                                  You don't need a library; the Javascript array type has a filter method:



                                  var filteredArray = myArray.filter(item => item !== undefined);





                                  share|improve this answer


























                                    1














                                    You don't need a library; the Javascript array type has a filter method:



                                    var filteredArray = myArray.filter(item => item !== undefined);





                                    share|improve this answer
























                                      1












                                      1








                                      1






                                      You don't need a library; the Javascript array type has a filter method:



                                      var filteredArray = myArray.filter(item => item !== undefined);





                                      share|improve this answer












                                      You don't need a library; the Javascript array type has a filter method:



                                      var filteredArray = myArray.filter(item => item !== undefined);






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 12 at 9:08









                                      stone

                                      4,5303859




                                      4,5303859























                                          1














                                          In filter function of lodash if the callback (you passed as argument) return truthy value that element considered to be retained in resultant array otherwise (in case return falsy) it will not retain that element. Your isEmpty return true if it is Empty and thus the result retains those values (null, undefined, 0, ...). So you can either use



                                          _.filter(myArray, _.negate(_.IsEmpty)) or _.filter(myArray, v => !_.IsEmpty(v))



                                          In the way you are trying



                                          Or, you can directly use _.filter(myArray) but in this case it will not remove empty object or empty array as same like _.filter(myArray, Boolean), passing Boolean is not necessery in case of using lodash



                                          in case you don't want to negate and want a simpler solution for removing all the empty element, then you can use



                                          _.reject(myArray, _.isEmpty)






                                          share|improve this answer




























                                            1














                                            In filter function of lodash if the callback (you passed as argument) return truthy value that element considered to be retained in resultant array otherwise (in case return falsy) it will not retain that element. Your isEmpty return true if it is Empty and thus the result retains those values (null, undefined, 0, ...). So you can either use



                                            _.filter(myArray, _.negate(_.IsEmpty)) or _.filter(myArray, v => !_.IsEmpty(v))



                                            In the way you are trying



                                            Or, you can directly use _.filter(myArray) but in this case it will not remove empty object or empty array as same like _.filter(myArray, Boolean), passing Boolean is not necessery in case of using lodash



                                            in case you don't want to negate and want a simpler solution for removing all the empty element, then you can use



                                            _.reject(myArray, _.isEmpty)






                                            share|improve this answer


























                                              1












                                              1








                                              1






                                              In filter function of lodash if the callback (you passed as argument) return truthy value that element considered to be retained in resultant array otherwise (in case return falsy) it will not retain that element. Your isEmpty return true if it is Empty and thus the result retains those values (null, undefined, 0, ...). So you can either use



                                              _.filter(myArray, _.negate(_.IsEmpty)) or _.filter(myArray, v => !_.IsEmpty(v))



                                              In the way you are trying



                                              Or, you can directly use _.filter(myArray) but in this case it will not remove empty object or empty array as same like _.filter(myArray, Boolean), passing Boolean is not necessery in case of using lodash



                                              in case you don't want to negate and want a simpler solution for removing all the empty element, then you can use



                                              _.reject(myArray, _.isEmpty)






                                              share|improve this answer














                                              In filter function of lodash if the callback (you passed as argument) return truthy value that element considered to be retained in resultant array otherwise (in case return falsy) it will not retain that element. Your isEmpty return true if it is Empty and thus the result retains those values (null, undefined, 0, ...). So you can either use



                                              _.filter(myArray, _.negate(_.IsEmpty)) or _.filter(myArray, v => !_.IsEmpty(v))



                                              In the way you are trying



                                              Or, you can directly use _.filter(myArray) but in this case it will not remove empty object or empty array as same like _.filter(myArray, Boolean), passing Boolean is not necessery in case of using lodash



                                              in case you don't want to negate and want a simpler solution for removing all the empty element, then you can use



                                              _.reject(myArray, _.isEmpty)







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Nov 12 at 9:39

























                                              answered Nov 12 at 9:32









                                              Koushik Chatterjee

                                              2,73731024




                                              2,73731024























                                                  0














                                                  var newArray = array.filter(arrayItem => arrayItem)


                                                  It will Filtering array so it would remove all undefined objects and assign to new variable called newArray






                                                  share|improve this answer























                                                  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
                                                    – Nick
                                                    Nov 12 at 10:25
















                                                  0














                                                  var newArray = array.filter(arrayItem => arrayItem)


                                                  It will Filtering array so it would remove all undefined objects and assign to new variable called newArray






                                                  share|improve this answer























                                                  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
                                                    – Nick
                                                    Nov 12 at 10:25














                                                  0












                                                  0








                                                  0






                                                  var newArray = array.filter(arrayItem => arrayItem)


                                                  It will Filtering array so it would remove all undefined objects and assign to new variable called newArray






                                                  share|improve this answer














                                                  var newArray = array.filter(arrayItem => arrayItem)


                                                  It will Filtering array so it would remove all undefined objects and assign to new variable called newArray







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Nov 12 at 11:04

























                                                  answered Nov 12 at 9:16









                                                  ishani shah

                                                  364




                                                  364












                                                  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
                                                    – Nick
                                                    Nov 12 at 10:25


















                                                  • While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
                                                    – Nick
                                                    Nov 12 at 10:25
















                                                  While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
                                                  – Nick
                                                  Nov 12 at 10:25




                                                  While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer - From Review
                                                  – Nick
                                                  Nov 12 at 10:25


















                                                  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.





                                                  Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                  Please pay close attention to the following guidance:


                                                  • 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%2f53258766%2ffiltering-array-so-it-would-remove-all-undefined-objects%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

                                                  Florida Star v. B. J. F.

                                                  Error while running script in elastic search , gateway timeout

                                                  Adding quotations to stringified JSON object values