Looping through an object and changing all values

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; }, {})


try

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

A nice solution is using a combination of Object.keys and reduce - which doesn't alter the original object;

var superSecret = function(spy){
  return Object.keys(spy).reduce(
    (attrs, key) => ({
      ...attrs,
      [key]: 'redacted',
    }),
    {}
  );
}

Here is a more simple version:

  Object.keys(spy).forEach(key => {
    spy[key] = 'redacted';
  });

Tags:

Javascript