Javascript loop an array to find numbers divisible by 3

前端 未结 8 1147
清歌不尽
清歌不尽 2021-01-25 05:41

I am needing to find the correct way to have javascript loop through an array, find all numbers that are divisible by 3, and push those numbers into a new array.

Here is

8条回答
  •  礼貌的吻别
    2021-01-25 06:15

    loveTheThrees=(arr)=>arr.filter(el=>Boolean(parseFloat(el)) && isFinite(el) && !Boolean(el%3))

    es6 version + skipping non numbers

    loveTheThrees([null,undefined,'haha',100,3,6])

    Result: [3,6]

提交回复
热议问题