I have a product that looks like this:
{
"productId": 5256502,
"name": "360fly - 4K Action Camera",
"light": true,
"wiFi": true,
"colour": "Black"
}
and I have some criteria that looks like this:
_criteria = [
{
"id": 38,
"type": "Boolean",
"name": "Light",
"states": [
{
"id": 93,
"criteriaId": 38,
"name": "False"
},
{
"id": 94,
"criteriaId": 38,
"name": "True"
}
]
},
{
"id": 41,
"type": "Boolean",
"name": "New",
"states": [
{
"id": 95,
"criteriaId": 41,
"name": "True"
},
{
"id": 96,
"criteriaId": 41,
"name": "False"
}
]
},
{
"id": 42,
"type": "Boolean",
"name": "Available",
"states": [
{
"id": 97,
"criteriaId": 42,
"name": "True"
},
{
"id": 98,
"criteriaId": 42,
"name": "False"
}
]
}
];
Criteria maps to a product by name. So for example, if I have a criteria called Light then I will should have a corresponding field called light in a product. So, I am trying to build a function that checks that out.
I was trying to loop through all the product properties like this:
// For each property in our product
for (var property in product) {
// If we have a property and it doesn't start with an underscore
if (product.hasOwnProperty(property) && property.substring(0, 1) !== '_' && property !== 'productId' && property !== 'name') {
}
}
and then I was going to loop through the criteria and match the name (after converting it to camelCase). But I need to compare it the other way around too, so I have to loop through all criteria and see if any exist in that that don't exist on a product.
I am hoping there is a nicer way of doing this than loop through all products, then looping through all criteria and matching and once that is done, loop through all criteria and then loop through all products.
Does anyone know of a way to help me out?