How to filter an object with attachments?

  • 0
    There is such an object:

    {
       "isLoading":false,
       "error":"",
       "product":"manual-text",
       "projectName":"",
       "existingBrief":"",
       "offlineData":{
          "text":"qwdqdqwdwq",
          "files":[]
       },
       "customerDetails":{
          "name":"qwdqwdw",
          "company":"qwdq",
          "position":"dqdqw",
          "email":"dqdd"
       },
       "profileObjectiveDetails":{
          "objective":"",
          "objectiveTargets":""
       },
       "entityCount":1,
       "objectives":[],
       "industries":"",
       "chapters":[],
       "chaptersUserSelection":[],
       "chaptersChoices":[],
       "pricing":{
          "currency":"",
          "options":[],
          "selection":{
             "token":"",
             "expertise":"",
             "deliveryKey":0
          }
       },
       "dataGrid":[],
       "target":"",
       "geo":{
          "type":"",
          "regions":{},
          "countries":[]
       },
       "finalNote":""
    }

    JavaScript Elijah Riley, Sep 22, 2019

  • 1 Answers
  • 0
    function filter(val) {
    if (!(val instanceof Object)) {
    return val;
    }

    const filtered = Object
    .entries(val)
    .map(n => [ n[0], filter(n[1]) ])
    .filter(n => n[1]);

    return filtered.length
    ? val instanceof Array
    ? filtered.map(n => n[1])
    : Object.fromEntries(filtered)
    : null;
    }
    Anonymous

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