skip to Main Content

I have a JSON which can look like this:

[
   {
      "categorySlug":"essential",
      "consents":[
         {
            "consentStatus":"true",
            "label":"Essential cookie"
         }
      ]
   },
   {
      "categorySlug":"tracking",
      "consents":[
         {
            "consentStatus":"true",
            "label":"Tracking cookie"
         }
      ]
   },
   {
      "categorySlug":"marketing",
      "consents":[
         {
            "consentStatus":"true",
            "label":"Marketing cookie 1"
         },
         {
            "consentStatus":"false",
            "label":"Marketing cookie 2"
         },
         {
            "consentStatus":"true",
            "label":"Marketing cookie 3"
         }
      ]
   }
]

The consentStatus can be either true or false.

I need to write a function which a different status based on the consentStatus from every categorySlug.

If every consentStatus is true then it returns ‘All consents are true’.

If the only consentStatus from the categorySlug essential is true, it returns ‘Essential’.

I’m not sure how this can be done in two for loops. I tried to create a new object, but it didn’t work. Or if anybody has a different idea.

Thanks!

categories.forEach(category => {
    category.consents.forEach(consent => {
        // Code for checks //
    })
})

4

Answers


  1. You can try by using flag variables like the following way:

    const categories = [
       {
          "categorySlug":"essential",
          "consents":[
             {
                "consentStatus":"true",
                "label":"Essential cookie"
             }
          ]
       },
       {
          "categorySlug":"tracking",
          "consents":[
             {
                "consentStatus":"true",
                "label":"Tracking cookie"
             }
          ]
       },
       {
          "categorySlug":"marketing",
          "consents":[
             {
                "consentStatus":"true",
                "label":"Marketing cookie 1"
             },
             {
                "consentStatus":"false",
                "label":"Marketing cookie 2"
             },
             {
                "consentStatus":"true",
                "label":"Marketing cookie 3"
             }
          ]
       }
    ];
    
    function checkStatus(categories) {
        let allTrue = true; //flag to track all consents
        let essentialTrue = false; //frag to track essential
    
        categories.forEach(category => {
            category.consents.forEach(consent => {
                if (consent.consentStatus === "false") {
                    allTrue = false; //if consentStatus false then set the flag to false
                }
                if (category.categorySlug === "essential" && consent.consentStatus === "true") {
                    essentialTrue = true; //if categorySlug is essential and consentStatus is false then set the flag to true
                }
            });
        });
        
        //return message based on the flag
        if (allTrue) {
            return "All consents are true";
        } else if (essentialTrue) {
            return "Essential";
        } else {
            return "Other";
        }
    }
    
    const res = checkStatus(categories);
    console.log(res);
    Login or Signup to reply.
  2. I think that what you are seeking is already implemented as the some() function, at least for checking that all consents are true. In addition, to check the essential you can simply use a variable

    You could utilize something along this way:

    const categories = [
        {
           "categorySlug":"essential",
           "consents":[
              {
                 "consentStatus":"true",
                 "label":"Essential cookie"
              }
           ]
        },
        {
           "categorySlug":"tracking",
           "consents":[
              {
                 "consentStatus":"true",
                 "label":"Tracking cookie"
              }
           ]
        },
        {
           "categorySlug":"marketing",
           "consents":[
              {
                 "consentStatus":"true",
                 "label":"Marketing cookie 1"
              },
              {
                 "consentStatus":"false",
                 "label":"Marketing cookie 2"
              },
              {
                 "consentStatus":"true",
                 "label":"Marketing cookie 3"
              }
           ]
        }
     ];
     categories.forEach(category => {
        let essential = undefined;
        if (category['categorySlug'] === 'essential') {
            essential = category['consents']['consentStatus'] === 'true';
        }
        const res = (category['consents'].some(element => {
          return element['consentStatus'] === 'false';
        }));
        if (res) {
          console.log('At least one is False');
          return;
        }
        if (essential) {
          console.log('Essentials are true');
          return;
        }
        console.log('All consents are true');
        return;
      });
    Login or Signup to reply.
  3. You can try something like below:

    essential = false;
    all = true;
    
    categories.forEach(category => {
    if(category.categorySlug == "essential"){
        essential = category.consents.consentStatus;
    }
    category.consents.forEach(consent => {
      if(consents.consentStatus == false){
          all = false;
          break;
      }
    })
    
    })
    return all ? "All consents are true" : (essential ? "Essential": "None")
    
    Login or Signup to reply.
  4. let essential = false, all = true;
    const check = item => item.consents.every(({consentStatus}) => consentStatus === 'true');
    
    for(const item of data){
      item.categorySlug === 'essential' ? essential = check(item) :  all = check(item);
      if(essential && !all){
        break;
      }
    }
    
    console.log(all ? 'All consents are true' : essential ? 'Essential' : 'No valid consent');
    <script>
    const data = [
       {
          "categorySlug":"tracking",
          "consents":[
             {
                "consentStatus":"true",
                "label":"Tracking cookie"
             }
          ]
       },
       {
          "categorySlug":"marketing",
          "consents":[
             {
                "consentStatus":"true",
                "label":"Marketing cookie 1"
             },
             {
                "consentStatus":"false",
                "label":"Marketing cookie 2"
             },
             {
                "consentStatus":"true",
                "label":"Marketing cookie 3"
             }
          ]
       },
       {
          "categorySlug":"essential",
          "consents":[
             {
                "consentStatus":"true",
                "label":"Essential cookie"
             }
          ]
       },
    ]
    </script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search