I have the following data:
let types = ["A", "C"];
let data = {
301: {
name: "Suite",
fields: [
[null, null, null, null, null, null],
[
null,
null,
{
type: "A",
},
{
type: "A",
},
null,
null,
],
[null, null, null, null, null, null],
[null, null, null, null, null, null],
[null, null, null, null, null, null],
[null, null, null, null, null, null],
[null, null, null, null, null, null],
],
},
302: {
name: "Suite",
fields: [
[null, null, null, null, null, null],
[
{
type: "B",
},
{
type: "B",
},
{
type: "B",
},
{
type: "B",
},
null,
null,
],
[null, null, null, null, null, null],
[null, null, null, null, null, null],
[null, null, null, null, null, null],
[null, null, null, null, null, null],
[null, null, null, null, null, null],
],
},
};
in the fields array there is either null or an object, if there is an object I want to check wether that object has a type that is included in types, if it isn't I want to set that array value to null aswell, otherwise just skip it.
How would I do this? Any help appreciated.
I tried this approach, which works but is really terrible:
Object.keys(data).forEach((key) => {
data[key].fields.forEach((fieldArray, fieldArrayIndex) => {
fieldArray.forEach((field, fieldIndex) => {
if (field) {
if (!types.includes(field.type)) {
fieldArray[fieldIndex] = null;
}
}
});
});
});