列表

详情


JS35. 接口

描述

请补全JavaScript代码,完成函数的接口功能。要求如下:
1. 函数接收两种类型的参数,分别为"get?"和"update?name=xxx&to=yyy","name"、"to"为参数,"xxx"、"yyy"分别为参数对应的值。
2. 当参数为"get?"时,返回data数据
3. 当参数为"update?name=xxx&to=yyy"时,将data中所有"name"为"xxx"的项,更改为"name"值为"yyy"

原站题解

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

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            let data = [\n                {name: 'nowcoder1'},\n                {name: 'nowcoder2'}\n            ]\n            \n            const _api = string => {\n                // 补全代码\n                let toArr = string.split('?');\n                if(toArr[0]=='get'){\n                    return data;\n                } else {\n                    let b = {};\n                    let a = toArr[1].split('&');\n                    a.forEach(item =>{\n                        let [key, value]=item.split('=');\n                        b[key]=value;\n                    })\n                    data.forEach((item,index) =>{\n                        if(item.name==b.name){\n                            data[index].name = b.to;\n                        }\n                    })\n                }\n            }\n        </script>\n    </body>\n</html>","libs":[]}

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

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            let data = [\n                {name: 'nowcoder1'},\n                {name: 'nowcoder2'}\n            ]\n            \n            const _api = string => {\n                // 补全代码\n                if (string === 'get?') {\n                    return data\n                } else {\n                    const str = string.split('?')[1]\n                    const strs = str.split('&')\n                    const name = strs[0].split('=')[1]\n                    const to = strs[1].split('=')[1]\n                    \n                    data.filter(item => item.name === name).forEach(item => {\n                        item.name = to\n                    })\n                    \n                    return data\n                }\n            }\n        </script>\n    </body>\n</html>","libs":[]}

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

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            let data = [\n                {name: 'nowcoder1'},\n                {name: 'nowcoder2'}\n            ]\n            \n            const _api = string => {\n                // 补全代码\n                let { 0: method, 1: queryStr } = string.split('?')\n                if (method == 'get') return data\n                let queryArr = queryStr.split('&')\n                let name = queryArr[0].split('=')[1]\n                let to = queryArr[1].split('=')[1]\n                for (let item of data) {\n                    if (item.name == name) {\n                        item.name = to\n                    }\n                }\n            }\n        </script>\n    </body>\n</html>","libs":[]}

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

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            let data = [\n                {name: 'nowcoder1'},\n                {name: 'nowcoder2'}\n            ]\n            \n            const _api = string => {\n                // 补全代码\n                let arr = string.split('?');\n                let type = arr[0];\n                if(type === 'get'){\n                    return data;\n                }else if(type === 'update'){\n                    let paramStr = arr[1].split('&');\n                    let param = {};\n                    paramStr.forEach(item => {\n                        let itemArr = item.split('=');\n                        param[itemArr[0]] = itemArr[1];\n                    });\n                    \n                    let currentData = data.find(d => {\n                        return d.name === param.name;\n                    });\n                    if(currentData){\n                        currentData.name = param.to;\n                    }\n                }\n            }\n        </script>\n    </body>\n</html>","libs":[]}

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

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            let data = [\n    { name: 'nowcoder1' },\n    { name: 'nowcoder2' }\n]\n\nconst _api = string => {\n    // 补全代码\n    let url = string.split(\"?\");\n    let type = url[0];\n    if (type === 'get') {\n        return data;\n    } else {\n        let params = url[1] ? url[1].split(\"&\") : [];\n        let p = {};\n        params.forEach((ele) => {\n            let obj = ele.split(\"=\");\n            p[obj[0]] = obj[1];\n        })\n        data.map((ele) => {\n            if (ele.name == p.name) {\n                ele.name = p.to;\n            }\n            return ele\n        })\n    }\n}\n\n        </script>\n    </body>\n</html>","libs":[]}

上一题