function weatherCodes(){ let codes = [{sunny:[1001]},{cloudy:[1002,1003]}] let theCode = 1003 };
如何检查我的对象数组以查找其中一个是否包含我的变量 theCode
P粉5174756702023-09-10 00:39:21
使用 flat()
和 includes()
为您提供的简单参考
function weatherCodes(){ let codes = [{sunny:[1001]},{cloudy:[1002,1003]}] let theCode = 1003 return codes.some(c => Object.values(c).flat().includes(theCode)); }; console.log(weatherCodes());
另一种选择
function weatherCodes(){ let codes = [{sunny:[1001]},{cloudy:[1002,1003]}] let theCode = 1003 let values = codes.flatMap(c => Object.values(c)).flat() return values.includes(theCode) }; console.log(weatherCodes());