Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript包括检查是否存在多个值_Javascript_Arrays_Loops_Object - Fatal编程技术网

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