Check for a value in an array of objects
P粉176151589
P粉176151589 2023-09-10 00:03:58
0
1
420

function weatherCodes(){
let codes = [{sunny:[1001]},{cloudy:[1002,1003]}]
let theCode = 1003
};

How do I check my array of objects to find if one of them contains my variable theCode

P粉176151589
P粉176151589

reply all(1)
P粉517475670

Using flat() and includes() Simple reference provided for you

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());

Another option

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());
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template