返回一个子数组,该子数组包含原始数组中所有比JavaScript中右边所有元素大的元素
我们需要编写一个JavaScript函数,该函数接受一个数字数组并返回一个子数组,该子数组包含原始数组中的所有元素,其大小大于其右侧的所有元素。
因此,让我们为该函数编写代码-
示例
为此的代码将是-
const arr = [12, 45, 6, 4, 23, 23, 21, 1];
const largerThanRight = (arr = []) => {
const creds = arr.reduceRight((acc, val) => {
let { largest, res } = acc;
if(val > largest){
res.push(val);
largest = val;
};
return { largest, res };
}, {
largest: -Infinity,
res: []
});
return creds.res;
};
console.log(largerThanRight(arr));输出结果
控制台中的输出将为-
[ 1, 21, 23, 45 ]
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短