Looping through an object and changing all values

后端 未结 7 1590
庸人自扰
庸人自扰 2020-12-29 21:42

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

7条回答
  •  醉梦人生
    2020-12-29 22:46

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

提交回复
热议问题