I have two arrays with objects in it which looks similar to:
let comp1 = [
{ optionKey: 'option1', displayName: '' },
{ optionKey: 'option2', displayName: '' },
{ optionKey: 'option3', displayName: '' },
{ optionKey: 'option4', displayName: '' },
{ optionKey: 'option5', displayName: '' },
{ optionKey: 'option6', displayName: '' },
{ optionKey: 'option7', displayName: '' },
];
let comp2 = [
{ option1Options: [] },
{ option2Options: [] },
{ option3Options: ['On', 'Off'] },
{ option4Options: ['Auto ', 'Off'] },
{ option5Options: [] },
{ option6Options: [] },
];
What I wanted to do was, if the value of each key in comp2's length in 0, I want to remove that key without Option index removed from comp1. So far, I was able to do is if each array has equal length
let comp1 = [
{ optionKey: 'option1', displayName: '' },
{ optionKey: 'option2', displayName: '' },
{ optionKey: 'option3', displayName: '' },
{ optionKey: 'option4', displayName: '' },
{ optionKey: 'option5', displayName: '' },
{ optionKey: 'option6', displayName: '' },
{ optionKey: 'option7', displayName: '' },
];
let comp2 = [
{ option1Options: [] },
{ option2Options: [] },
{ option3Options: ['On', 'Off'] },
{ option4Options: ['Auto ', 'Off'] },
{ option5Options: [] },
{ option6Options: [] },
];
comp1.forEach(function (item) {
var index = comp2.findIndex(function (item2, i) {
return item2[item.optionKey + 'Options'] !== undefined;
});
if (index !== -1) {
if (comp2[index][item.optionKey + 'Options'].length === 0) {
comp1.splice(index, 1);
}
}
});
console.log(comp1)
But in my case comp1 and comp2 length are different. How should I remove it by the key's value in comp1? Any help on this is apricated.