列表

详情


FED8. Array.filter

描述

请补全JavaScript代码,要求实现Array.filter函数的功能且该新函数命名为"_filter"。

示例1

输入:

[1,2]._filter(i => i>1)

输出:

[2]

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1683ms, 内存消耗: 77804KB, 提交时间: 2022-02-09

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n\n        <script type=\"text/javascript\">\n            // 补全代码\n            // arr.filter((item) => )\n            Array.prototype._filter = function (fn) {\n                if (typeof fn !== 'function') {\n                    throw new TypeError('should be function')\n                }\n                let array = this\n                let len = array.length\n                let res = []\n                for (let i = 0; i < len; i++) {\n                    let isTrue = fn.call(arguments[1], array[i], i, array)\n                    if (isTrue) {\n                        res.push(array[i])\n                    }\n                }\n                return res\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1684ms, 内存消耗: 77816KB, 提交时间: 2022-02-09

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n\n        <script type=\"text/javascript\">\n            // 补全代码\n            Array.prototype._filter = function(fn) {\n                if(typeof fn !== 'function') {\n                    return\n                }\n                const array = this\n                const newArray = []\n                for(let i = 0 ; i < array.length ; i++) {\n                    let result = fn.call(arguments[1],array[i],i,array)\n                    result && newArray.push(array[i])\n                }\n                return newArray\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1739ms, 内存消耗: 77808KB, 提交时间: 2021-12-05

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n\n        <script type=\"text/javascript\">\n            // 补全代码\n            Array.prototype._filter = function(fn){\n                let newArr = [];\n                this.forEach(item => {\n                    if(fn(item)){\n                        newArr.push(item);\n                    }\n                })\n                return newArr;\n            }\n            console.log([1,2]._filter(i => i>1));\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1740ms, 内存消耗: 77772KB, 提交时间: 2021-12-08

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n\n        <script type=\"text/javascript\">\n            // 补全代码\n            Array.prototype._filter=function(fun){\n                let result=[];\n                this.forEach((ele)=>{\n                    if(fun(ele)){\n                        result.push(ele)\n                    }\n                })\n                return result\n            }\n            \n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1741ms, 内存消耗: 77776KB, 提交时间: 2022-01-25

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n\n        <script type=\"text/javascript\">\n            // 补全代码\n            Array.prototype._filter = function(fn){\n                let arr = []\n                this.forEach(item=>{\n                    if(fn(item)){\n                        arr.push(item)\n                    }\n                })\n                return arr\n            }\n        </script>\n    </body>\n</html>","libs":[]}

上一题