How to traverse an object in depth leaving only the required fields as a result?

  • 0
    How to traverse an object in depth leaving only the required fields as a result? I don’t understand how I can go through the recursion writing the values, so that as a result of the function's work, the object will be returned from only the necessary values.

    function filter (obj) {
      let keys = Object.keys(obj);
      let key = keys.shift();
    
      if (keys.length === 0) return obj;
    
      if (!key.match(/_/gi)) {
        return filter(obj[key]);
      }
    
    }
    
    const obj = {
      promo: {
        koto: {
          moto: 'sef',
          _mlol: {
            set: 'asd'
          }
        }
      },
      _moto: 'sef',
      mlol: {
        set: { _dodo: 'ts'}
      }
    }
    JavaScript Anonymous, Oct 18, 2019

  • 0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!