Hi
I am not that familiar with JavaScript. I am familiar with python. I wanted to know how I could set up an if statement that checks if certain value is in an array (or dictionary in python. I don’t know if it’s called dictionaries in Javascript)
I tried the following but it didn’t work:
$w.onReady( () => {
let itemObj = $w("#dynamicDataset").getCurrentItem();
wixData.queryReferenced("Kapperszaken", itemObj._id, "opHours")
.then( (results) => {
if(results.items.length > 0) {
console.log(results.items)
let hours = results.items;
console.log(hours)
if ("ma10" in hours) {
$w("#checkbox1").checked = true
$w("#checkbox1").disable()
}
} else {
$w("#text24").text = "nothing found";
}
} )
.catch( (err) => {
let errorMsg = err;
$w("#text24").text = "error";
} );
});
The array prints the following in the console:
Array(1)
0:
"{\"_id\":\"c639f89b-715d-41be-a4ef-4bf7aef68c96\",\"_owner\":\"e80b8559-8e52-44a1-a9a6-bbb2e15d5e56\",\"_createdDate\":\"2020-09-12T07:52:42.351Z\",\"ma1230\":true,\"_updatedDate\":\"2020-09-12T08:09:51.335Z\",\"ma10\":true,\"title\":\"dddd\",\"ma9\":true}"
Is anyone able to help?
Thanks
Jhon
J.D
September 12, 2020, 5:44pm
2
To check if an array includes a certain value, you can use the JS .includes() method, and if it’s an array of objects you can use the .some() method.
The some() method tests whether
at least one element in the array passes the test implemented by the provided
function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns...
Thanks! I tried this but it didn’t work:
let itemObj = $w("#dynamicDataset").getCurrentItem();
wixData.queryReferenced("Kapperszaken", itemObj._id, "opHours")
.then( (results) => {
if(results.items.length > 0) {
console.log(results.items)
let hours = results.items;
console.log(hours)
let ma9 = hours.includes("ma9");
if (ma9) {
$w("#checkbox1").checked = true;
}
let ma930 = hours.includes("ma930");
if (ma930) {
$w("#checkbox8").checked = true;
}
let ma10 = hours.includes("ma10");
if (ma9) {
$w("#checkbox15").checked = true;
}
let ma1030 = hours.includes("ma1030");
if (ma1030) {
$w("#checkbox22").checked = true;
}
} else {
$w("#text24").text = "nothing found";
}
} )
.catch( (err) => {
let errorMsg = err;
$w("#text24").text = "error";
} );
But it just doesn’t work. Any idea why?
UPDATE: It returns everything false. Even if it’s in the array.
J.D
September 12, 2020, 5:55pm
5
@js4150563 because it’s an array of objects. I guess you wanted to do something like:
let hours = results.items.map(e => e.hours);//replace e.hours by the relevant field key
@jonatandor35 Thanks! But now it returns everything true even the ones not in there.
J.D
September 12, 2020, 6:20pm
7
@js4150563 sorry I haven’t looked at the array results you pasted above. So it looks you have a separate field to each of them.
So:
let hours = results.items;
if(hours.some(e => e.ma9)){
//etc...,
}
Ok, I really appreciate your help :).
I tried what you said, but it didn’t really work out. here’s the code:
$w.onReady( () => {
let itemObj = $w("#dynamicDataset").getCurrentItem();
wixData.queryReferenced("Kapperszaken", itemObj._id, "opHours")
.then( (results) => {
if(results.items.length > 0) {
console.log(results.items)
let hours = results.items;
console.log(hours)
if(hours.some(e => "ma9")){
$w("#checkbox1").checked = true;
}
if (hours.some(e => "ma930")) {
$w("#checkbox8").checked = true;
}
if (hours.some(e => "ma10")) {
$w("#checkbox15").checked = true;
}
if (hours.some(e => "ma1030")) {
$w("#checkbox22").checked = true;
}
} else {
$w("#text24").text = "nothing found";
}
} )
.catch( (err) => {
let errorMsg = err;
$w("#text24").text = "error";
} );
});
It does all the if statements. Even the ones who are supposed to be false.
When I try to add the e before the string it gives me an ‘unexpected token error’
if (hours.some(e => e."ma10")) {
J.D
September 12, 2020, 6:31pm
9
if (hours.some(e => e.ma10))
NOT:
if (hours.some(e => e."ma10"))
NOT:
if (hours.some(e => "ma10"))
@jonatandor35 IT WORKS! Thank you so much!