- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了一个函数getMax
,它模拟Math.max,可以从一组数字中获取最大数字。该函数接受可变参数。
我使用Array.prototype.slice(arguments)
将它们转换为真正的数组。但我失败了,我得到一个空数组[]。如果我使用 Array.from(arguments) 我将得到一个正确的数组。我不知道为什么 Array.prototype.slice(arguments)
的传统方式对我不起作用。
此函数的另一个问题是,在我获得正确的参数数组后,getMax
的返回值为 undefined
,但我确实得到了
让我很困惑。filterMax
函数中的返回值 7
function getMax() {
"use strict";
let filterMax = function(arr) {
let maxValue = arr[0];
let resultArr = arr.filter(function(value) {
return value > maxValue;
});
if (resultArr.length == 0) {
return maxValue; //output: 7
} else {
resultArr = filterMax(resultArr);
}
};
let args = Array.from(arguments); //output: [ 3, 7, 2, 5, 1, 4 ]
// let args = Array.prototype.slice(arguments); //output: []
console.log(args); //output: []
return filterMax(args); //output: undefined
}
console.log(getMax(3, 7, 2, 5, 1, 4)); //output: undefined
最佳答案
Array.prototype.slice(arguments)
基本相同
[].slice(arguments)
(除了第一种情况下 this
是 Array.prototype
,但这或多或少等于在空数组上调用它)
... 返回一个空数组,因为从空数组进行切片将始终导致空数组。您可能想做:
Array.prototype.slice.call(arguments)
调用 .slice
与 this
是arguments
,因此它会产生所需的数组,但我更喜欢 Array.from(arguments)
或[...arguments]
,或者更好的是,一个剩余参数:
function findMax(...numbers) {
//...
}
Another problem with this function is that after I get a correct arguments array, the return value of getMax is undefined, but I do get a return value 7 in filterMax function which confuses me
嗯,那是因为:
let maxValue = arr[0];
let resultArr = arr.filter(function(value) {
return value > maxValue;
});
过滤掉大于第一个数组元素的任何数组元素(在您的示例中,5和7大于3),因此代码进入else分支...
resultArr = filterMax(resultArr);
...并且什么也不返回(又名 undefined
)。您可能想要
return filterMax(resultArr);
总而言之:
function getMax(...numbers) {
function filterMax(numbers) {
const first = numbers[0];
const bigger = numbers.filter(function(value) {
return value > first;
});
if (bigger.length == 0) {
return first;
} else {
return filterMax(bigger);
}
};
return filterMax(numbers);
}
console.log(getMax(3, 7, 2, 5, 1, 4)); //output: 7
关于javascript - 为什么 "Array.prototype.slice(arguments)"给我一个空的 "[]"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57743921/
试图自学,但发现很难找到示例,我的大脑已经陷入了困境。非常不确定 3 和 4,需要帮助才能使 5 正常工作。 package main import "fmt" func main () {
我有一个 slice ,它由字符串类型的 slice 组成。我希望能够为这片 slice 的各个元素赋值,不一定按顺序。然后,稍后,我希望能够更改任何特定元素的值。我已经阅读了有关 slice 的相同
我正在尝试将整数 slice append 到由整数 slice 组成的 slice 。当我打印 slice 时,它按预期显示。但是,当我将 slice append 到一片 slice 时,内容会发
我读了go slice usage and internals和 Slice和 Effective go#slice但是没有像这样用 3 个数字 slice 的方法:slice[a:b:c] 例如这段
这个问题在这里已经有了答案: Are slices passed by value? (5 个答案) 关闭 8 个月前。 如果可能的话,我正在努力使我的代码更具性能以节省内存 我做了一些研究,但找不
我是 Golang 的新手。 当我尝试它时,出现编译错误: cannot use a.B (type []*C) as type []Z in field value 代码: package main
我有数据结构: type PosList []int type InvertedIndex struct { Capacity int Len int IndexList [
我在 Go 中使用矩阵乘法进行一些性能实验并遇到了一些意想不到的结果。 版本 1: func newMatrix(n int) [][]int { m := make([][]int, n)
文档涵盖了 slice() 的 3 种用法对象: obj[:stop] = obj[slice(stop)] obj[start:stop] = obj[slice(start, stop)] obj
我有以下表示网页的结构 type Webpage struct { url.URL references []url.URL } 我想将网站表示为网页的集合。我正在使用这个结构,但感觉
我有一个这样的结构: type Headers struct { header string valueFromCalculation string
我正在参观 Golang 网站,我正在尝试消化其中一个示例。目前还不清楚它是如何工作的: package main import "fmt" func main() { s := []int{
我很好奇解包 slice 并将它们作为参数发送给可变参数函数。 假设我们有一个带有可变参数的函数: func unpack(args ...interface{}) 如果我们不想传入它工作的接口(in
我正在尝试解码来自服务器的 gzip 响应,该响应是一个 msgpack 数组或最终被 gzip 压缩的 msgpack 数组。 为了说明这一点,我的回复看起来像这样: gzip(msgpack([m
我是 Go 编程的新手。我在 Go 编程书籍中读到 slice 由三部分组成:指向数组的指针、长度和容量。 我很困惑: nil slice ( slice 没有可指向的底层数组,len = 0,cap
在Go Programming Language书中,作者给出了append()函数的以下代码示例,该函数接受[]int和int作为参数,并将相应地处理调整大小: // gopl.io/ch4/app
我在代码高尔夫游戏中尝试优化字符串复数时遇到了这个怪癖。我的想法是将字符串写成复数形式,然后使用 substr 有条件地切断最后一个字符: var counter = 1; var myText =
我有一个字符串数组:slice1 [][]string。我使用 for 循环获得了我想要的值: for _, i := range slice1 { //[string1 string2] f
我正在尝试实现一个将 TCP 端口 slice 拆分为 x 个其他 slice 的功能。这些 slice 将发送给将扫描这些端口的工作人员,因此 x 由工作人员的数量设置。 这是代码: // crea
我有以下代码 func Sum(a []int) int { res := 0 for _, n := range a { res += n } ret
我是一名优秀的程序员,十分优秀!