Looping through an object and changing all values











up vote
1
down vote

favorite
1












I'm having trouble looping through an object and changing all the values to something else, let's say I want to change all the values to the string "redacted". I need to be able to do this in pure JavaScript.



For example I'd have an object like this...



spy = {
id: 007,
name: "James Bond",
age: 31
};


and the object would look like this after...



spy = {
id: "redacted",
name: "redacted",
age: "redacted"
};


Here is what I have to start with



var superSecret = function(spy){
// Code Here
}


This shouldn't create a new spy object but update it.










share|improve this question






















  • for...in loop
    – Rayon
    Apr 7 '16 at 5:26






  • 1




    You can use Object.keys(spy) to get the property names, then loop over that using say forEach. Lots of questions and answers here on that.
    – RobG
    Apr 7 '16 at 5:30

















up vote
1
down vote

favorite
1












I'm having trouble looping through an object and changing all the values to something else, let's say I want to change all the values to the string "redacted". I need to be able to do this in pure JavaScript.



For example I'd have an object like this...



spy = {
id: 007,
name: "James Bond",
age: 31
};


and the object would look like this after...



spy = {
id: "redacted",
name: "redacted",
age: "redacted"
};


Here is what I have to start with



var superSecret = function(spy){
// Code Here
}


This shouldn't create a new spy object but update it.










share|improve this question






















  • for...in loop
    – Rayon
    Apr 7 '16 at 5:26






  • 1




    You can use Object.keys(spy) to get the property names, then loop over that using say forEach. Lots of questions and answers here on that.
    – RobG
    Apr 7 '16 at 5:30















up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





I'm having trouble looping through an object and changing all the values to something else, let's say I want to change all the values to the string "redacted". I need to be able to do this in pure JavaScript.



For example I'd have an object like this...



spy = {
id: 007,
name: "James Bond",
age: 31
};


and the object would look like this after...



spy = {
id: "redacted",
name: "redacted",
age: "redacted"
};


Here is what I have to start with



var superSecret = function(spy){
// Code Here
}


This shouldn't create a new spy object but update it.










share|improve this question













I'm having trouble looping through an object and changing all the values to something else, let's say I want to change all the values to the string "redacted". I need to be able to do this in pure JavaScript.



For example I'd have an object like this...



spy = {
id: 007,
name: "James Bond",
age: 31
};


and the object would look like this after...



spy = {
id: "redacted",
name: "redacted",
age: "redacted"
};


Here is what I have to start with



var superSecret = function(spy){
// Code Here
}


This shouldn't create a new spy object but update it.







javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 7 '16 at 5:25









Dominic

69111




69111












  • for...in loop
    – Rayon
    Apr 7 '16 at 5:26






  • 1




    You can use Object.keys(spy) to get the property names, then loop over that using say forEach. Lots of questions and answers here on that.
    – RobG
    Apr 7 '16 at 5:30




















  • for...in loop
    – Rayon
    Apr 7 '16 at 5:26






  • 1




    You can use Object.keys(spy) to get the property names, then loop over that using say forEach. Lots of questions and answers here on that.
    – RobG
    Apr 7 '16 at 5:30


















for...in loop
– Rayon
Apr 7 '16 at 5:26




for...in loop
– Rayon
Apr 7 '16 at 5:26




1




1




You can use Object.keys(spy) to get the property names, then loop over that using say forEach. Lots of questions and answers here on that.
– RobG
Apr 7 '16 at 5:30






You can use Object.keys(spy) to get the property names, then loop over that using say forEach. Lots of questions and answers here on that.
– RobG
Apr 7 '16 at 5:30














5 Answers
5






active

oldest

votes

















up vote
13
down vote



accepted










try



var superSecret = function(spy){
Object.keys(spy).forEach(function(key){ spy[key] = "redacted" });
return spy;
}





share|improve this answer























  • Man giving me an error of function returned undefined :(
    – Dominic
    Apr 7 '16 at 5:32






  • 1




    Just return spy;
    – Rayon
    Apr 7 '16 at 5:32










  • Just face palmed myself. I appreciate the help! Been a while since I've played with JavaScript.
    – Dominic
    Apr 7 '16 at 5:34










  • @RayonDabre thanks, though OP gave no indication of whether he wants to use the retured value :)
    – gurvinder372
    Apr 7 '16 at 5:42










  • @gurvinder372, My comment was for OP as he was expecting something in return ;)
    – Rayon
    Apr 7 '16 at 5:44


















up vote
1
down vote













var superSecret = function(spy){
for(var key in spy){
if(spy.hasOwnProperty(key)){
//code here
spy[key] = "redacted";
}
}
return spy;
}





share|improve this answer




























    up vote
    1
    down vote













    You can also go functional.



    Using Object.keys is better as you will only go through the object properties and not it's prototype chain.



    Object.keys(spy).reduce((acc, key) => {acc[key] = 'redacted'; return acc; }, {})






    share|improve this answer




























      up vote
      0
      down vote













      I wrote a little helper function that walks through an object and applies a callback to each entry:



      iterateEntries(node, fn) {
      const newNode = {};
      Object.entries(node).forEach(([key, val]) => (newNode[key] = fn(val)));
      return newNode;
      }


      Usage:



      iterateEntries(yourObject, (entry) => {
      return entry; // do something with entry here
      });





      share|improve this answer




























        up vote
        -1
        down vote













        Use a proxy:



        function superSecret(spy) {
        return new Proxy(spy, { get() { return "redacted"; } });
        }

        > superSecret(spy).id
        < "redacted"





        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',
          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%2f36467369%2flooping-through-an-object-and-changing-all-values%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          13
          down vote



          accepted










          try



          var superSecret = function(spy){
          Object.keys(spy).forEach(function(key){ spy[key] = "redacted" });
          return spy;
          }





          share|improve this answer























          • Man giving me an error of function returned undefined :(
            – Dominic
            Apr 7 '16 at 5:32






          • 1




            Just return spy;
            – Rayon
            Apr 7 '16 at 5:32










          • Just face palmed myself. I appreciate the help! Been a while since I've played with JavaScript.
            – Dominic
            Apr 7 '16 at 5:34










          • @RayonDabre thanks, though OP gave no indication of whether he wants to use the retured value :)
            – gurvinder372
            Apr 7 '16 at 5:42










          • @gurvinder372, My comment was for OP as he was expecting something in return ;)
            – Rayon
            Apr 7 '16 at 5:44















          up vote
          13
          down vote



          accepted










          try



          var superSecret = function(spy){
          Object.keys(spy).forEach(function(key){ spy[key] = "redacted" });
          return spy;
          }





          share|improve this answer























          • Man giving me an error of function returned undefined :(
            – Dominic
            Apr 7 '16 at 5:32






          • 1




            Just return spy;
            – Rayon
            Apr 7 '16 at 5:32










          • Just face palmed myself. I appreciate the help! Been a while since I've played with JavaScript.
            – Dominic
            Apr 7 '16 at 5:34










          • @RayonDabre thanks, though OP gave no indication of whether he wants to use the retured value :)
            – gurvinder372
            Apr 7 '16 at 5:42










          • @gurvinder372, My comment was for OP as he was expecting something in return ;)
            – Rayon
            Apr 7 '16 at 5:44













          up vote
          13
          down vote



          accepted







          up vote
          13
          down vote



          accepted






          try



          var superSecret = function(spy){
          Object.keys(spy).forEach(function(key){ spy[key] = "redacted" });
          return spy;
          }





          share|improve this answer














          try



          var superSecret = function(spy){
          Object.keys(spy).forEach(function(key){ spy[key] = "redacted" });
          return spy;
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Apr 7 '16 at 5:42

























          answered Apr 7 '16 at 5:26









          gurvinder372

          52.8k53355




          52.8k53355












          • Man giving me an error of function returned undefined :(
            – Dominic
            Apr 7 '16 at 5:32






          • 1




            Just return spy;
            – Rayon
            Apr 7 '16 at 5:32










          • Just face palmed myself. I appreciate the help! Been a while since I've played with JavaScript.
            – Dominic
            Apr 7 '16 at 5:34










          • @RayonDabre thanks, though OP gave no indication of whether he wants to use the retured value :)
            – gurvinder372
            Apr 7 '16 at 5:42










          • @gurvinder372, My comment was for OP as he was expecting something in return ;)
            – Rayon
            Apr 7 '16 at 5:44


















          • Man giving me an error of function returned undefined :(
            – Dominic
            Apr 7 '16 at 5:32






          • 1




            Just return spy;
            – Rayon
            Apr 7 '16 at 5:32










          • Just face palmed myself. I appreciate the help! Been a while since I've played with JavaScript.
            – Dominic
            Apr 7 '16 at 5:34










          • @RayonDabre thanks, though OP gave no indication of whether he wants to use the retured value :)
            – gurvinder372
            Apr 7 '16 at 5:42










          • @gurvinder372, My comment was for OP as he was expecting something in return ;)
            – Rayon
            Apr 7 '16 at 5:44
















          Man giving me an error of function returned undefined :(
          – Dominic
          Apr 7 '16 at 5:32




          Man giving me an error of function returned undefined :(
          – Dominic
          Apr 7 '16 at 5:32




          1




          1




          Just return spy;
          – Rayon
          Apr 7 '16 at 5:32




          Just return spy;
          – Rayon
          Apr 7 '16 at 5:32












          Just face palmed myself. I appreciate the help! Been a while since I've played with JavaScript.
          – Dominic
          Apr 7 '16 at 5:34




          Just face palmed myself. I appreciate the help! Been a while since I've played with JavaScript.
          – Dominic
          Apr 7 '16 at 5:34












          @RayonDabre thanks, though OP gave no indication of whether he wants to use the retured value :)
          – gurvinder372
          Apr 7 '16 at 5:42




          @RayonDabre thanks, though OP gave no indication of whether he wants to use the retured value :)
          – gurvinder372
          Apr 7 '16 at 5:42












          @gurvinder372, My comment was for OP as he was expecting something in return ;)
          – Rayon
          Apr 7 '16 at 5:44




          @gurvinder372, My comment was for OP as he was expecting something in return ;)
          – Rayon
          Apr 7 '16 at 5:44












          up vote
          1
          down vote













          var superSecret = function(spy){
          for(var key in spy){
          if(spy.hasOwnProperty(key)){
          //code here
          spy[key] = "redacted";
          }
          }
          return spy;
          }





          share|improve this answer

























            up vote
            1
            down vote













            var superSecret = function(spy){
            for(var key in spy){
            if(spy.hasOwnProperty(key)){
            //code here
            spy[key] = "redacted";
            }
            }
            return spy;
            }





            share|improve this answer























              up vote
              1
              down vote










              up vote
              1
              down vote









              var superSecret = function(spy){
              for(var key in spy){
              if(spy.hasOwnProperty(key)){
              //code here
              spy[key] = "redacted";
              }
              }
              return spy;
              }





              share|improve this answer












              var superSecret = function(spy){
              for(var key in spy){
              if(spy.hasOwnProperty(key)){
              //code here
              spy[key] = "redacted";
              }
              }
              return spy;
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Apr 7 '16 at 5:32









              Himanshu Tanwar

              826414




              826414






















                  up vote
                  1
                  down vote













                  You can also go functional.



                  Using Object.keys is better as you will only go through the object properties and not it's prototype chain.



                  Object.keys(spy).reduce((acc, key) => {acc[key] = 'redacted'; return acc; }, {})






                  share|improve this answer

























                    up vote
                    1
                    down vote













                    You can also go functional.



                    Using Object.keys is better as you will only go through the object properties and not it's prototype chain.



                    Object.keys(spy).reduce((acc, key) => {acc[key] = 'redacted'; return acc; }, {})






                    share|improve this answer























                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      You can also go functional.



                      Using Object.keys is better as you will only go through the object properties and not it's prototype chain.



                      Object.keys(spy).reduce((acc, key) => {acc[key] = 'redacted'; return acc; }, {})






                      share|improve this answer












                      You can also go functional.



                      Using Object.keys is better as you will only go through the object properties and not it's prototype chain.



                      Object.keys(spy).reduce((acc, key) => {acc[key] = 'redacted'; return acc; }, {})







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 6 at 12:39









                      Harshal

                      12010




                      12010






















                          up vote
                          0
                          down vote













                          I wrote a little helper function that walks through an object and applies a callback to each entry:



                          iterateEntries(node, fn) {
                          const newNode = {};
                          Object.entries(node).forEach(([key, val]) => (newNode[key] = fn(val)));
                          return newNode;
                          }


                          Usage:



                          iterateEntries(yourObject, (entry) => {
                          return entry; // do something with entry here
                          });





                          share|improve this answer

























                            up vote
                            0
                            down vote













                            I wrote a little helper function that walks through an object and applies a callback to each entry:



                            iterateEntries(node, fn) {
                            const newNode = {};
                            Object.entries(node).forEach(([key, val]) => (newNode[key] = fn(val)));
                            return newNode;
                            }


                            Usage:



                            iterateEntries(yourObject, (entry) => {
                            return entry; // do something with entry here
                            });





                            share|improve this answer























                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              I wrote a little helper function that walks through an object and applies a callback to each entry:



                              iterateEntries(node, fn) {
                              const newNode = {};
                              Object.entries(node).forEach(([key, val]) => (newNode[key] = fn(val)));
                              return newNode;
                              }


                              Usage:



                              iterateEntries(yourObject, (entry) => {
                              return entry; // do something with entry here
                              });





                              share|improve this answer












                              I wrote a little helper function that walks through an object and applies a callback to each entry:



                              iterateEntries(node, fn) {
                              const newNode = {};
                              Object.entries(node).forEach(([key, val]) => (newNode[key] = fn(val)));
                              return newNode;
                              }


                              Usage:



                              iterateEntries(yourObject, (entry) => {
                              return entry; // do something with entry here
                              });






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 8 at 14:12









                              Lukas

                              2,35943050




                              2,35943050






















                                  up vote
                                  -1
                                  down vote













                                  Use a proxy:



                                  function superSecret(spy) {
                                  return new Proxy(spy, { get() { return "redacted"; } });
                                  }

                                  > superSecret(spy).id
                                  < "redacted"





                                  share|improve this answer



























                                    up vote
                                    -1
                                    down vote













                                    Use a proxy:



                                    function superSecret(spy) {
                                    return new Proxy(spy, { get() { return "redacted"; } });
                                    }

                                    > superSecret(spy).id
                                    < "redacted"





                                    share|improve this answer

























                                      up vote
                                      -1
                                      down vote










                                      up vote
                                      -1
                                      down vote









                                      Use a proxy:



                                      function superSecret(spy) {
                                      return new Proxy(spy, { get() { return "redacted"; } });
                                      }

                                      > superSecret(spy).id
                                      < "redacted"





                                      share|improve this answer














                                      Use a proxy:



                                      function superSecret(spy) {
                                      return new Proxy(spy, { get() { return "redacted"; } });
                                      }

                                      > superSecret(spy).id
                                      < "redacted"






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Apr 7 '16 at 6:07

























                                      answered Apr 7 '16 at 5:35







                                      user663031





































                                          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%2f36467369%2flooping-through-an-object-and-changing-all-values%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







                                          這個網誌中的熱門文章

                                          Hercules Kyvelos

                                          Tangent Lines Diagram Along Smooth Curve

                                          Yusuf al-Mu'taman ibn Hud