简体   繁体   English

ES6其余参数参数示例

[英]ES6 rest parameters arguments example

I don't really understand the below JavaScript example. 我不太了解下面的JavaScript示例。

function containsAll(arr) {
  for (let k = 1; k < arguments.length; k++) {
    let num = arguments[k];
    if (arr.indexOf(num) === -1) {
      return false;
    }
  }
  return true;
}
let x = [2, 4, 6, 7];
console.log(containsAll(x, 2, 4, 7));
console.log(containsAll(x, 6, 4, 9));

The output is 1 and 0 in the console. 在控制台中,输出为10

I'm trying to envision how it should work. 我正在尝试设想它应该如何工作。

  1. In this console.log(containsAll(x, 2, 4, 7)), the x should be replaced, changing it to console.log(containsAll(2, 4, 6, 7, 2, 4, 7)). 在此console.log(containsAll(x,2,4,4,7))中,应替换x ,将其更改为console.log(containsAll(2,4,6,7,2,4,7))。
  2. The function containsAll get those numbers (2, 4, 6, 7, 2, 4, 7). function containsAll获取那些数字(2、4、6、7、2、4、7)。
  3. In this line, for(let k = 1; k < arguments.length; k++) , k should be a new array [1, 2, 3, 4, 5, 6], with the argument length which is the length of arr (in this case, 7 is the length), right? 在这一行中, for(let k = 1; k < arguments.length; k++)k应该是一个新数组[1、2、3、4、5、6],其参数长度是arr的长度(在这种情况下,长度为7),对吗?

  4. In next step, which is let num = arguments[k]; if (arr.indexOf(num) === -1) {return false;} 在下一步中, let num = arguments[k]; if (arr.indexOf(num) === -1) {return false;} let num = arguments[k]; if (arr.indexOf(num) === -1) {return false;} let num should be num = 1 , right? let num = arguments[k]; if (arr.indexOf(num) === -1) {return false;} let num应该为num = 1 ,对吧?

  5. Then, in the if statement, it tests if 1 is in the array, arr = [2, 4, 6, 7, 2, 4, 7]. 然后,在if语句中,它测试数组中是否为1 ,arr = [2,4,6,7,2,4,4,7]。 And if no match is found, it returns false. 如果找不到匹配项,则返回false。 And it should repeat for the next number in the arr array, correct? 并且应该对arr数组中的下一个数字重复,对吗?

I'm just trying to figure out why it output 1 for console.log(containsAll(x, 2, 4, 7)); 我只是想弄清楚为什么它为console.log(containsAll(x, 2, 4, 7));输出1 console.log(containsAll(x, 2, 4, 7)); when it should return something like [false, true, false, true, false, true] . 当它应该返回类似[false, true, false, true, false, true]

  1. In this console.log(containsAll(x, 2, 4, 7)), the x should be replaced, changing it to console.log(containsAll(2, 4, 6, 7, 2, 4, 7)). 在此console.log(containsAll(x,2,4,4,7))中,应替换x ,将其更改为console.log(containsAll(2,4,6,7,2,4,7))。

No, it stays [2, 4, 6, 7], 2 ,4 ,7 不,它保持[2, 4, 6, 7], 2 ,4 ,7

  1. The function containsAll get those numbers (2, 4, 6, 7, 2, 4, 7). function containsAll获取那些数字(2、4、6、7、2、4、7)。

It gets [2, 4, 6, 7], 2 ,4 ,7 它得到[2, 4, 6, 7], 2 ,4 ,7

  1. In this line, for(let k = 1; k < arguments.length; k++) , k should be a new array [1, 2, 3, 4, 5, 6], with the argument length which is the length of arr (in this case, 7 is the length), right? 在这一行中, for(let k = 1; k < arguments.length; k++)k应该是一个新数组[1、2、3、4、5、6],其参数长度是arr的长度(在这种情况下,长度为7),对吗?

k is just a number representing the index inside arguments not an array. k只是一个数字,表示arguments而不是数组中的索引。 The length is just 4, the array is just one element 长度只有4,数组只是一个元素

  1. In next step, which is let num = arguments[k]; if (arr.indexOf(num) === -1) {return false;} 在下一步中, let num = arguments[k]; if (arr.indexOf(num) === -1) {return false;} let num = arguments[k]; if (arr.indexOf(num) === -1) {return false;} let num should be num = 1 , right? let num = arguments[k]; if (arr.indexOf(num) === -1) {return false;} let num应该为num = 1 ,对吧?

num iterates over the remaining elements of arguments ( 2 ,4 ,7 ). num迭代的其余元件arguments2 ,4 ,7 )。 Notice the k=1 in the for loop to skip the first element. 请注意,for循环中的k=1会跳过第一个元素。

  1. Then, in the if statement, it tests if 1 is in the array, arr = [2, 4, 6, 7, 2, 4, 7]. 然后,在if语句中,它测试数组中是否为1 ,arr = [2,4,6,7,2,4,4,7]。 And if no match is found, it returns false. 如果找不到匹配项,则返回false。 And it should repeat for the next number in the arr array, correct? 并且应该对arr数组中的下一个数字重复,对吗?

It returns already if it finds the first match. 如果找到第一个匹配项,则它已经返回。

I'm just trying to figure out why it output 1 for console.log(containsAll(x, 2, 4, 7)); 我只是想弄清楚为什么它为console.log(containsAll(x, 2, 4, 7));输出1 console.log(containsAll(x, 2, 4, 7)); when it should return something like [false, true, false, true, false, true] . 当它应该返回类似[false, true, false, true, false, true]

When return is called the execution inside the method stops. 调用return ,方法内的执行将停止。

First, your code currently outputs `true` and `false` not 1 and 0.
The reason you don't have an array like this,[false, true, false, true, false, true], is because of the `return` keyword used in the conditional `if` statement inside your `for` loop.

The code below is one way to do it. 下面的代码是一种实现方法。

function containsAll(arr) {
  let result = []
  for (let k = 1; k < arguments.length; k++) {
    let num = arguments[k];
    if (arr.indexOf(num) === -1) {
      result.push(false);
    } else{
        result.push(true);
    }
  }
    return result;
}
let x = [2, 4, 6, 7];
console.log(containsAll(x, 2, 4, 7)); //Output [true, true, true]
console.log(containsAll(x, 6, 4, 9)); //Output [true, true, false]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM