在 JavaScript 中查找数组中的上部元素
javascriptweb developmentfront end technologyobject oriented programming更新于 2026/2/4 18:20:17
我们需要编写一个 JavaScript 函数,该函数将数字数组作为第一个参数,将单个数字作为第二个参数。该函数应返回输入数组中所有大于或等于作为第二个参数的数字的元素的数组。
因此,让我们编写该函数的代码 −
示例
其代码为 −
const arr = [56, 34, 2, 7, 76, 4, 45, 3, 3, 34, 23, 2, 56, 5];
const threshold = 40;
const findGreater = (arr, num) => {
const res = [];
for(let i = 0; i < arr.length; i++){
if(arr[i] < num){
continue;
};
res.push(arr[i]);
};
return res;
};
console.log(findGreater(arr, threshold));
输出
控制台中的输出将是 −
[ 56, 76, 45, 56 ]
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

