I have the following object for example:
{
"name":"false",
"var_RadiacionGlobalInt":true,
"var_CO2InvVaisala":true,
"var_RadiacionExt":false,
"var_VVExt":false
}
I would need to iterate through that object and add the elements that are true to a new array with a push. This is the code I have implemented:
public guardar(){
this.checkboxsensores = [];
for (let variable in this.checkbox) {
if (variable === 'true') {
this.checkboxsensores.push(variable);
}
}
}
I need a final format like this:
["var_RadiacionGlobalInt", "var_CO2InvVaisala"]
I can't perform the search for the true value only by the name of the field, how can I filter by booleans?
All the best.
One option would be to use Object.keys to obtain the properties of the object and then filter using filter() which will create a new one
array
with the properties that have the valuetrue
obj[el]===true
Dev. Joel's answer is better, but for you to understand what is wrong in your code, you have to check the value of the property and not the name of the property itself. Your corrected code would look like this: