在对象中搜索

Search in Object

我有这个对象:

combineLatest( 
this.toppings.valueChanges.pipe(startWith(this.toppings.value)),
this.toppings2.valueChanges.pipe(startWith(this.toppings2.value)))
.subscribe(val => console.log(val))


(2) [Array(9), Array(2)]
0:(9) ["Douleur", "Psychologie", "Nutrition", "Social", "Rééducation physique", "Hygiène de vie", "Sexualité et fertilité", "Soins palliatifs", "Oncogériatrie"]

1:(2) ["CHU Nantes", "CH Le Mans"]

length:2
__proto__:Array(0)

我怎样才能做到像这样的情况? if ("CHU Nantes" is in the object val)if ("Douleur" or "Psychologie" is in the object val), 所以...

我在结果中尝试了这个:

if (val.includes("CHU Nantes")) { 
 CHUNantes.addTo(myfrugalmap);
} else { 
  CHUNantes.remove();
};
console.log(val);

您需要展平数组以搜索所有值。例如:val.reduce((acc, val) => acc.concat(val), []).includes("CHU Nantes");

对于现代浏览器,您还可以使用 Array.prototype.flat:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat#Browser_compatibility