简体   繁体   English

给定包含数字和字符串的数组,Javascript返回数字小于100

[英]Javascript return number less than 100 given an array with numbers and strings

Given below, how do I write a function that will return a number less than 100? 如下所示,我该如何编写一个返回小于100的数字的函数?

const myArray = ['hello', 3, true, 18, 10,, 99 'ten', false]

const isLessThan100 = (array) => {
  // how to do this? Solution enter here
}

I think it involves the filter method, but im not sure how to filter both a number less than 100, and is not a string. 我认为它涉及filter方法,但是我不确定如何同时过滤小于100的数字和不是字符串的数字。

Thanks in advance! 提前致谢!

you can check if it is a number first like this 您可以先检查它是否是数字

const myArray = ['hello', 3, true, 18, 10, 99, 'ten', false];

const isLessThan100 = myArray.filter(item => {
  return (typeof item === "number") && item < 100;
});

The typeof operator returns a string indicating the type of the unevaluated operand. typeof运算符返回一个字符串,该字符串指示未评估的操作数的类型。

You can first check whether the typeof the item is number or not, then check if it is less than 100 . 你可以先检查是否typeof的产品number或没有,然后检查它是否小于100

You can reduce the code to a single line by removing the curly braces. 您可以通过删除花括号将代码减少到一行。

Try Array.prototype.filter() like the following way: 尝试使用Array.prototype.filter() ,方法如下:

 const myArray = ['hello', 3, true, 18, 10,, 99, 'ten', false] const isLessThan100 = (array) => array.filter(num => typeof(num) === "number" && num < 100); console.log(isLessThan100(myArray)) 
 const isLessThan100 = (array) 

Here's a short-ish one using filter : 这是一个使用filter的矮个子:

 const myArray = ['hello', 3, true, 18, 10, 99, 101, 'ten', false]; const isLessThan100 = a => a.filter(e => +e === e && e < 100); console.log(isLessThan100(myArray)); 

For getting only a single value, you could reduce the array. 为了只获取一个值,可以减少数组。

 const array = ['hello', 3, true, 18, 10,, 99, 'ten', false], isLessThan100 = array => array.reduce((r, v) => typeof v === 'number' && v < 100 && (typeof r !== 'number' || v > r) ? v : r, undefined); console.log(isLessThan100(array)); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用于生成小于给定数字的素数的 Javascript 生成器 - Javascript generator to generate prime numbers less than a given number 将数字数组拆分为总和小于或等于给定数字的数组 - Split an array of numbers to arrays whose sum is less or equal than a given number Javascript或jQuery接受小于100的数字的文本字段 - Javascript or jQuery to accept numbers less than 100 for text field 我需要在 javascript 中创建一个函数来显示有多少个数组成员小于给定的数字 - I need to create a function in javascript to show how many members of array are less than a given number 查找数组中有多少项大于等于或小于给定数字 Javascript - Find how many items in array are greater equal or less than given number Javascript 如果字符串小于特定数字,则返回布尔值 - Return boolean if a string is less than a certain number JavaScript 指令接受大于0且小于100的数字 - Directive to accept numbers greater than 0 and less than 100 正则表达式允许十进制数大于0且小于100 - Regex to allow decimal numbers greater than 0 and less than 100 检查数组中的任何数字是否大于给定的数字 Javascript - Check if any number in array is bigger than given number Javascript Javascript 寻找长度小于给定值的最佳组合(最少项目数)的算法 - Javascript Algorithm to find the best combination(least number of items) of lengths less than a given value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM