javascript包括检查是否存在多个值
这是javascript包括检查是否存在多个值,javascript,arrays,loops,object,Javascript,Arrays,Loops,Object,这是colorsarray。我可以检查colors数组中是否存在一个值。例如,在这里,我想检查颜色数组中是否存在红色。我使用下面的代码 const colors = ["black", "red", "pink", ]; 所以我想要一个标志来知道颜色数组中是否存在红色、白色或蓝色。我如何做到这一点?对此有什么建议吗?因此,您可以使用另一个数组。解决方案取决于您实际需要什么 const colors=[“黑色”、“红色”、“粉色”] 常量checkFor=[“红色”、“白色”] const h
colors
array。我可以检查colors数组中是否存在一个值。例如,在这里,我想检查颜色数组中是否存在红色。我使用下面的代码
const colors = ["black", "red", "pink", ];
所以我想要一个标志来知道颜色数组中是否存在红色、白色或蓝色。我如何做到这一点?对此有什么建议吗?因此,您可以使用另一个数组。解决方案取决于您实际需要什么
const colors=[“黑色”、“红色”、“粉色”]
常量checkFor=[“红色”、“白色”]
const hasAll=checkFor.every(color=>colors.includes(color))
const hasSome=checkFor.some(color=>colors.includes(color))
const included=checkFor.filter(颜色=>colors.includes(颜色))
const excluded=checkFor.filter(颜色=>!colors.includes(颜色))
const checks=checkFor.reduce((obj,color)=>({[color]:colors.includes(color),…obj}),{})
console.log('hasAll',hasAll)
console.log('hasSome',hasSome)
console.log('included',included)
console.log('excluded',excluded)
console.log('checks',checks,checks.red)
另一个数组带有some
或every
您所说的“想要一个标志”是什么意思?
Const IsRedExist = colors.includes("red"); //returns true