Как будем определять, должен ли элемент массива остаться:
function mustStay(n) {
for (const m of arr2) {
if (m.content.insuranceType === n.value) {
return false;
}
}
return true;
}
// или
const mustStay = n => arr2.every(m => m.content.insuranceType !== n.value);
// или
const mustStay = function(n) {
return !this.has(n.value);
}.bind(new Set(arr2.map(n => n.content.insuranceType)));
// или
const mustStay = n =>
(function next(i) {
return i >= arr2.length
? true
: arr2[i].content.insuranceType !== n.value && next(-~i);
})(0);
Удаляем неподходящие:
for (let i = 0; i < arr1.length; i++) {
if (!mustStay(arr1[i])) {
for (let j = i--; ++j < arr1.length; arr1[j - 1] = arr1[j]) ;
arr1.pop();
}
}
// или
arr1.reduceRight((_, n, i, a) => mustStay(n) || a.splice(i, 1), null);
// или
arr1.splice(0, arr1.length, ...arr1.filter(mustStay));
// или
arr1.length -= arr1.reduce((acc, n, i, a) => (
a[i - acc] = n,
acc + !mustStay(n)
), 0);