My data is structured in the following way:
const Items = [{"Name":"type1","Options":[1,2,5]},{"Name":"type2","Options":[1,2]},{"Name":"type1","Options":[2,5]}];
Although I am new to javascript, I am trying to identify the common options associated with each type name.
The number of elements in the Items array can vary. It might be 40 for example.
If we consider the above data, my expected output would look like this:
CommonOptions = [{"Name":"type1","Options":[2,5]},{"Name":"type2","Options":[1,2]}];
This is because 2 and 5 are common to all items with name type1, while 1 and 2 are common among items with name type2. However, I am unsure about how to correctly access this data.
I have made progress so far. Any guidance on the right direction would be greatly appreciated.
const Items = [{
"Name": "type1",
"Options": [1, 2, 5]
}, {
"Name": "type2",
"Options": [1, 2]
}, {
"Name": "type1",
"Options": [2, 5]
}];
let CommonOptions = [];
CommonOptions.push(Items[0]);
for (let i = 1, iLen = Items.length - 1; i < iLen; i++) {
for (let j = 0, cLen = CommonOptions.length; j < cLen; j++) {
if (CommonOptions[j].Name.includes(Items[i].Name)) {
CommonOptions.push(Items[i]);
} else {
// Check Options array for common values
}
}
}
console.log(CommonOptions);