在对象中搜索

问题描述 投票:0回答:1

我有这个对象:

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);
angular typescript rxjs subscribe
1个回答
1
投票

您需要展平数组以搜索所有值。例如: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

© www.soinside.com 2019 - 2024. All rights reserved.