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
You can try by using flag variables like the following way:
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 theessential
you can simply use a variableYou could utilize something along this way:
You can try something like below: