Vue.js: extracting data from a nested object using for…in

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I have a getter in my vuex module which gets data from a nested object, then check if there is any key match to the string and push these matched objects into an array. ESLint says that for..in loops iterate over the entire prototype chain, which is virtually never what I want. Its suggests to use Object.keys,values,entries, and iterate over the resulting array.
How can I refactor this?



getUsersData( users: data: users ) 
if (users)
const arr = Object.keys(users.stats
return ;







share|improve this question



























    up vote
    1
    down vote

    favorite












    I have a getter in my vuex module which gets data from a nested object, then check if there is any key match to the string and push these matched objects into an array. ESLint says that for..in loops iterate over the entire prototype chain, which is virtually never what I want. Its suggests to use Object.keys,values,entries, and iterate over the resulting array.
    How can I refactor this?



    getUsersData( users: data: users ) 
    if (users)
    const arr = Object.keys(users.stats
    return ;







    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a getter in my vuex module which gets data from a nested object, then check if there is any key match to the string and push these matched objects into an array. ESLint says that for..in loops iterate over the entire prototype chain, which is virtually never what I want. Its suggests to use Object.keys,values,entries, and iterate over the resulting array.
      How can I refactor this?



      getUsersData( users: data: users ) 
      if (users)
      const arr = Object.keys(users.stats
      return ;







      share|improve this question













      I have a getter in my vuex module which gets data from a nested object, then check if there is any key match to the string and push these matched objects into an array. ESLint says that for..in loops iterate over the entire prototype chain, which is virtually never what I want. Its suggests to use Object.keys,values,entries, and iterate over the resulting array.
      How can I refactor this?



      getUsersData( users: data: users ) 
      if (users)
      const arr = Object.keys(users.stats
      return ;









      share|improve this question












      share|improve this question




      share|improve this question








      edited May 23 at 9:29









      Igor Soloydenko

      2,697827




      2,697827









      asked May 23 at 8:28









      Olga B

      234




      234




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Your original question has a partial answer here I believe. In fact, ESLint is telling you the exact method(s) to use, which is Object.keys(el), for example.




          Aside from that, there are a few semi-related things you can refactor...




          • Object.keys(users.stats || ).map(key => users.stats[key]) is essentially the same as Object.values(users.stats || ).

          ...as well as unrelated ones:



          • Names like arr, el, newArr, obj, key, and str are extremely abstract, and dealing with them in real code forces spending brain cycles on decrypting their meaning instead of understanding the intent of the code. In fact, I would hesitate to post a question before renaming the variable into something meaningful. See, the readers don't know the complete structure of the object(s) you're working with and it complicates the review process.

          • Unnecessary temporary objects may be avoided, if map(), filter(), reduce() and other chaining-friendly functions are used together.

          • A guarding construction if (!users) return is more idiomatic for JavaScript/TypeScript and reduces the nesting (fewer s and s).


          The result may look something like this. I'm sorry if I missed something — I don't know how to reliably test that my refactoring went well. Plus, it's a late night in my timezone now, so my brain acts funny. :)



          getUsersData( users: data: users ) 
          if (!users) return ;

          return Object.values(users.stats





          share|improve this answer





















          • Thank you! Definitely, you gave me very good advise. I changed my code and it still works. :)
            – Olga B
            May 23 at 11:01










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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: "196"
          ;
          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195002%2fvue-js-extracting-data-from-a-nested-object-using-for-in%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          Your original question has a partial answer here I believe. In fact, ESLint is telling you the exact method(s) to use, which is Object.keys(el), for example.




          Aside from that, there are a few semi-related things you can refactor...




          • Object.keys(users.stats || ).map(key => users.stats[key]) is essentially the same as Object.values(users.stats || ).

          ...as well as unrelated ones:



          • Names like arr, el, newArr, obj, key, and str are extremely abstract, and dealing with them in real code forces spending brain cycles on decrypting their meaning instead of understanding the intent of the code. In fact, I would hesitate to post a question before renaming the variable into something meaningful. See, the readers don't know the complete structure of the object(s) you're working with and it complicates the review process.

          • Unnecessary temporary objects may be avoided, if map(), filter(), reduce() and other chaining-friendly functions are used together.

          • A guarding construction if (!users) return is more idiomatic for JavaScript/TypeScript and reduces the nesting (fewer s and s).


          The result may look something like this. I'm sorry if I missed something — I don't know how to reliably test that my refactoring went well. Plus, it's a late night in my timezone now, so my brain acts funny. :)



          getUsersData( users: data: users ) 
          if (!users) return ;

          return Object.values(users.stats





          share|improve this answer





















          • Thank you! Definitely, you gave me very good advise. I changed my code and it still works. :)
            – Olga B
            May 23 at 11:01














          up vote
          1
          down vote



          accepted










          Your original question has a partial answer here I believe. In fact, ESLint is telling you the exact method(s) to use, which is Object.keys(el), for example.




          Aside from that, there are a few semi-related things you can refactor...




          • Object.keys(users.stats || ).map(key => users.stats[key]) is essentially the same as Object.values(users.stats || ).

          ...as well as unrelated ones:



          • Names like arr, el, newArr, obj, key, and str are extremely abstract, and dealing with them in real code forces spending brain cycles on decrypting their meaning instead of understanding the intent of the code. In fact, I would hesitate to post a question before renaming the variable into something meaningful. See, the readers don't know the complete structure of the object(s) you're working with and it complicates the review process.

          • Unnecessary temporary objects may be avoided, if map(), filter(), reduce() and other chaining-friendly functions are used together.

          • A guarding construction if (!users) return is more idiomatic for JavaScript/TypeScript and reduces the nesting (fewer s and s).


          The result may look something like this. I'm sorry if I missed something — I don't know how to reliably test that my refactoring went well. Plus, it's a late night in my timezone now, so my brain acts funny. :)



          getUsersData( users: data: users ) 
          if (!users) return ;

          return Object.values(users.stats





          share|improve this answer





















          • Thank you! Definitely, you gave me very good advise. I changed my code and it still works. :)
            – Olga B
            May 23 at 11:01












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Your original question has a partial answer here I believe. In fact, ESLint is telling you the exact method(s) to use, which is Object.keys(el), for example.




          Aside from that, there are a few semi-related things you can refactor...




          • Object.keys(users.stats || ).map(key => users.stats[key]) is essentially the same as Object.values(users.stats || ).

          ...as well as unrelated ones:



          • Names like arr, el, newArr, obj, key, and str are extremely abstract, and dealing with them in real code forces spending brain cycles on decrypting their meaning instead of understanding the intent of the code. In fact, I would hesitate to post a question before renaming the variable into something meaningful. See, the readers don't know the complete structure of the object(s) you're working with and it complicates the review process.

          • Unnecessary temporary objects may be avoided, if map(), filter(), reduce() and other chaining-friendly functions are used together.

          • A guarding construction if (!users) return is more idiomatic for JavaScript/TypeScript and reduces the nesting (fewer s and s).


          The result may look something like this. I'm sorry if I missed something — I don't know how to reliably test that my refactoring went well. Plus, it's a late night in my timezone now, so my brain acts funny. :)



          getUsersData( users: data: users ) 
          if (!users) return ;

          return Object.values(users.stats





          share|improve this answer













          Your original question has a partial answer here I believe. In fact, ESLint is telling you the exact method(s) to use, which is Object.keys(el), for example.




          Aside from that, there are a few semi-related things you can refactor...




          • Object.keys(users.stats || ).map(key => users.stats[key]) is essentially the same as Object.values(users.stats || ).

          ...as well as unrelated ones:



          • Names like arr, el, newArr, obj, key, and str are extremely abstract, and dealing with them in real code forces spending brain cycles on decrypting their meaning instead of understanding the intent of the code. In fact, I would hesitate to post a question before renaming the variable into something meaningful. See, the readers don't know the complete structure of the object(s) you're working with and it complicates the review process.

          • Unnecessary temporary objects may be avoided, if map(), filter(), reduce() and other chaining-friendly functions are used together.

          • A guarding construction if (!users) return is more idiomatic for JavaScript/TypeScript and reduces the nesting (fewer s and s).


          The result may look something like this. I'm sorry if I missed something — I don't know how to reliably test that my refactoring went well. Plus, it's a late night in my timezone now, so my brain acts funny. :)



          getUsersData( users: data: users ) 
          if (!users) return ;

          return Object.values(users.stats






          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered May 23 at 9:25









          Igor Soloydenko

          2,697827




          2,697827











          • Thank you! Definitely, you gave me very good advise. I changed my code and it still works. :)
            – Olga B
            May 23 at 11:01
















          • Thank you! Definitely, you gave me very good advise. I changed my code and it still works. :)
            – Olga B
            May 23 at 11:01















          Thank you! Definitely, you gave me very good advise. I changed my code and it still works. :)
          – Olga B
          May 23 at 11:01




          Thank you! Definitely, you gave me very good advise. I changed my code and it still works. :)
          – Olga B
          May 23 at 11:01












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195002%2fvue-js-extracting-data-from-a-nested-object-using-for-in%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation