0

I'm building a function that uses a for-in loop to go through any object, change it's values (whether they be a number, string or boolean) to a single specific string, then output it. I've been through a bunch of existing questions which have helped me gain a general understanding of for-in loops. I've been trying to figure out how to make the function make the change then return the new values. Here's what I have so far:

var switcharoo = function(whateverObject){
  for (var i in whateverObject){
    if (whateverObject.hasOwnProperty(i)){
  alert(i + "redacted" + whateverObject[i]);
    }
  }
return whateverObject;
};    

I understand the framework I've set up for the function. I understand the structure of the for-in loop. I also understand the importance of the hasOwnProperty part. Now I'm lost... the

alert(i + "this is a string" + whateverObject[i]);

line, I can't wrap my head around. I got it from a question here on StackOverflow. The return whateverObject line is just my best effort to output the result. So how would I run through an object and change all the values to a specific string?

Bonus question: How would I tweak this function to search for true/false values and remove only the false ones?

Zong
  • 6,160
  • 5
  • 32
  • 46
Kyle
  • 5
  • 4
  • 2
    You are not assigning any string to any of the object properties. Besides i would suggest using Object.keys() for simplicity. – Redu Apr 29 '16 at 01:39

2 Answers2

0

The problem is... you're not changing anything at all :D

alert just displays the value, but does nothing else.

var switcharoo = function(whateverObject){
  for (var i in whateverObject){
    if (whateverObject.hasOwnProperty(i)){
      alert(i + "redacted" + whateverObject[i]);

      // Assign value to this key in whateverObject
      whateverObject[i] = 'redacted';

    }
  }
  return whateverObject;
}; 

How would I tweak this function to search for true/false values and remove only the false ones?

Now for some advanced JavaScript. You can use Object.keys to get an array of an object's keys. Use that array to loop through all the keys in the object. You can then use reduce to construct an object with only the keys that don't have false values.

function switcharoo(object){
  return Object.keys(object).reduce(function(allTrue, key){
    if(object[key] !== false) allTrue[key] = object[key];
    return allTrue;
  }, {});
}
Joseph
  • 117,725
  • 30
  • 181
  • 234
  • so you use reduce to rake through all of an objects 'keys' using whatever stipulations you create (allTrue, in this instance) and only work with whatever meets those stipulations? – Kyle Apr 29 '16 at 02:04
  • @Kyle `reduce` is like `forEach` except you get the return value of the previous iteration. In this case, it is used to carry through the initial value `{}` as `allTrue`, appending each non-false key-value pair along the way. The last iteration's return becomes the return of `reduce`. – Joseph Apr 29 '16 at 02:09
  • Got it, I'm still getting used to the idea that you can declare a new variable (i.e. allTrue) in the middle of a block of code. If a key has no value, is it false? I'm getting an error still because one of the keys has no value and is included in my output when it shouldn't... – Kyle Apr 29 '16 at 02:25
  • @Kyle `allTrue` didn't come from thin air. It's the `{}` at the end, the "initial value" of `reduce`. Also, instead of checking for `false`, why not compare to `true` (`object[key] === true`). – Joseph Apr 29 '16 at 02:27
0

Another approach

var switcharoo = (o,s) => Object.keys(o).reduce((p,k) => {p[k] = s; return p},{}),
         myObj = {a: 1,
                  b: _ => console.log(this.a),
                  c: {x: 3,
                      y: "hi there",
                      z: [1,2,3]
                     }
                 };
document.write("<pre>" + JSON.stringify(switcharoo(myObj, "stractassineboom"), null, 2) + "</pre>");
Redu
  • 25,060
  • 6
  • 56
  • 76