列表

详情


FED61. 添加元素

描述

在数组 arr 开头添加元素 item。不要直接修改数组 arr,结果返回新的数组

示例1

输入:

[1, 2, 3, 4], 10

输出:

[10, 1, 2, 3, 4]

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 860ms, 内存消耗: 77856KB, 提交时间: 2020-11-27

{"css":"","js":"function prepend(arr, item) {\n    let newArr = arr.slice(0);\n    newArr.unshift(item);\n    return newArr;\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 863ms, 内存消耗: 77796KB, 提交时间: 2020-12-22

{"css":"","js":"function prepend(arr, item) {\n    let arr2 = arr.filter((value, index, ar) => {\n        return value\n    })\n    arr2.unshift(item)\n    return arr2\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 864ms, 内存消耗: 77792KB, 提交时间: 2020-11-23

{"css":"","js":"\nfunction prepend(arr, item) {\n    var Arr = arr.slice();\n    Arr.unshift(item);\n    return Arr;\n}\n","html":"","libs":[]}

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

{"css":"","js":"function prepend(arr, item) {\n    var newarr=arr.slice(0)\n    newarr.unshift(item)\n    return newarr\n}","html":"","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 864ms, 内存消耗: 77812KB, 提交时间: 2020-12-21

{"css":"","js":"function prepend(arr, item) {\n    let arr1 = JSON.parse(JSON.stringify(arr))\n    arr1.unshift(item)\n    return arr1\n}","html":"","libs":[]}

上一题