检查数组是否包含(仅)数字值 [英] Check if an array contains (only) numeric values
本文介绍了检查数组是否包含(仅)数字值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有诸如这样的数组
var arrayVal_Int = ["21", "53", "92", "79"];
var arrayVal_Alpha = ["John", "Christine", "Lucy"];
var arrayVal_AlphaNumeric = ["CT504", "AP308", "NK675"];
-
arrayVal_Int
以上的字符应被视为(纯)数字. -
arrayVal_Alpha
和arrayVal_AlphaNumeric
应该被视为字符串. - Above
arrayVal_Int
should be considered as (purely) numeric. arrayVal_Alpha
andarrayVal_AlphaNumeric
should be considered as strings.
我需要在JavaScript中进行检查.
I need to check that in JavaScript.
推荐答案
最短的解决方案,当且仅当每一项都是(可强制)数字时,才评估为true
:
Shortest solution, evals to true
if and only if every item is (coercible to) a number:
!yourArray.some(isNaN)
这篇关于检查数组是否包含(仅)数字值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文