Javascript:指定数组';s长度和调用函数

Javascript:指定数组';s长度和调用函数,javascript,Javascript,我有一个函数,它在数组中搜索指定的数字,并返回搜索的值、数组的长度和数组所在的索引 我有3个问题: 有没有办法创建一个调用我的console.log的函数,这样我就不必每次创建新数组时都编写它们 在这种情况下,如何获取数组的第一个值?(my console.log中的“第一个数组元素”) 有没有一种方法可以让我的数组有值,比如说1到“数字”,而不需要输入中间的所有值?因为如果我的范围在1-400之间,输入所有这些值都会伤害我的手指;( 代码: 功能包括(arr、obj){ 对于(变量i=0;i

我有一个函数,它在数组中搜索指定的数字,并返回搜索的值、数组的长度和数组所在的索引

我有3个问题:

  • 有没有办法创建一个调用我的console.log的函数,这样我就不必每次创建新数组时都编写它们
  • 在这种情况下,如何获取数组的第一个值?(my console.log中的“第一个数组元素”)
  • 有没有一种方法可以让我的数组有值,比如说1到“数字”,而不需要输入中间的所有值?因为如果我的范围在1-400之间,输入所有这些值都会伤害我的手指;(
  • 代码:

    功能包括(arr、obj){
    对于(变量i=0;i
    首先:你的意思是这样的吗

    function report(a) {
      console.log("You searched for the value: " + a[1]);
      console.log("The length of the array is: " + a[2]);
      console.log("The Index of the value(" + a[1] + ") from " + "'first array element'" + " to " + (a[2]) + " is: " + a[0]);
    }
    
    其次,要获取第一个数组元素,您可以访问arr[0]。为什么不从include()返回arr本身,就像这样

    function include(arr, obj) {
      for(var i=0; i< arr.length; i++) {
        if (arr[i] == obj)
          return [i, obj, arr];
      }
    }
    

    至于定义数字的范围,JS没有本机的方法,但是您可以使用下划线库的u.range()方法,或者扩展Javascript的数组,比如!

    首先:您的意思是这样的吗

    function report(a) {
      console.log("You searched for the value: " + a[1]);
      console.log("The length of the array is: " + a[2]);
      console.log("The Index of the value(" + a[1] + ") from " + "'first array element'" + " to " + (a[2]) + " is: " + a[0]);
    }
    
    其次,要获取第一个数组元素,您可以访问arr[0]。为什么不从include()返回arr本身,就像这样

    function include(arr, obj) {
      for(var i=0; i< arr.length; i++) {
        if (arr[i] == obj)
          return [i, obj, arr];
      }
    }
    

    至于定义数字的范围,JS没有本机的方法,但是您可以使用下划线库的u.range()方法,或者扩展Javascript的数组,比如!

    首先:您的意思是这样的吗

    function report(a) {
      console.log("You searched for the value: " + a[1]);
      console.log("The length of the array is: " + a[2]);
      console.log("The Index of the value(" + a[1] + ") from " + "'first array element'" + " to " + (a[2]) + " is: " + a[0]);
    }
    
    其次,要获取第一个数组元素,您可以访问arr[0]。为什么不从include()返回arr本身,就像这样

    function include(arr, obj) {
      for(var i=0; i< arr.length; i++) {
        if (arr[i] == obj)
          return [i, obj, arr];
      }
    }
    

    至于定义数字的范围,JS没有本机的方法,但是您可以使用下划线库的u.range()方法,或者扩展Javascript的数组,比如!

    首先:您的意思是这样的吗

    function report(a) {
      console.log("You searched for the value: " + a[1]);
      console.log("The length of the array is: " + a[2]);
      console.log("The Index of the value(" + a[1] + ") from " + "'first array element'" + " to " + (a[2]) + " is: " + a[0]);
    }
    
    其次,要获取第一个数组元素,您可以访问arr[0]。为什么不从include()返回arr本身,就像这样

    function include(arr, obj) {
      for(var i=0; i< arr.length; i++) {
        if (arr[i] == obj)
          return [i, obj, arr];
      }
    }
    
    至于定义数字的范围,JS没有本机的方法,但您可以使用下划线库的u.range()方法,或扩展Javascript的数组,如!

    每个问题:

  • 函数内部的console.log。在函数末尾返回搜索变量。注意从循环到
    indexOf
    的更改
  • 您可以使用作为搜索条件的
    obj
    参数和
    array.length
    来获取数组的长度。我改进了使用
    array.indexOf
    的功能,消除了循环的需要

  • for循环将执行以下操作:

  • var newArray=[];
    对于(变量i=0;i<400;i++)
    {
    array.push(i+1);//将向数组中推送400个值。
    }
    
    每个问题:

  • 函数内部的console.log。在函数末尾返回搜索变量。注意从循环到
    indexOf
    的更改
  • 您可以使用作为搜索条件的
    obj
    参数和
    array.length
    来获取数组的长度。我改进了使用
    array.indexOf
    的功能,消除了循环的需要

  • for循环将执行以下操作:

  • var newArray=[];
    对于(变量i=0;i<400;i++)
    {
    array.push(i+1);//将向数组中推送400个值。
    }
    
    每个问题:

  • 函数内部的console.log。在函数末尾返回搜索变量。注意从循环到
    indexOf
    的更改
  • 您可以使用作为搜索条件的
    obj
    参数和
    array.length
    来获取数组的长度。我改进了使用
    array.indexOf
    的功能,消除了循环的需要

  • for循环将执行以下操作:

  • var newArray=[];
    对于(变量i=0;i<400;i++)
    {
    array.push(i+1);//将向数组中推送400个值。
    }
    
    每个问题:

  • 函数内部的console.log。在函数末尾返回搜索变量。注意从循环到
    indexOf
    的更改
  • 您可以使用作为搜索条件的
    obj
    参数和
    array.length
    来获取数组的长度。我改进了使用
    array.indexOf
    的功能,消除了循环的需要

  • for循环将执行以下操作:

  • var newArray=[];
    对于(变量i=0;i<400;i++)
    {
    array.push(i+1);//将向数组中推送400个值。
    }