Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 不了解使用每个和映射的循环_Javascript_Arrays - Fatal编程技术网

Javascript 不了解使用每个和映射的循环

Javascript 不了解使用每个和映射的循环,javascript,arrays,Javascript,Arrays,我知道每个和映射都有本机函数(我想),但我们正在学习幕后发生的事情。那么看看这个 var sampleInput = [ [1, 3, 2], [4, 23, 100], [7, 6, 3, -2] ]; function eachNew(coll, f) { if (Array.isArray(coll)) { for (var i = 0; i < coll.length; i++) { f(coll[i], i); } } els

我知道每个和映射都有本机函数(我想),但我们正在学习幕后发生的事情。那么看看这个

var sampleInput = [ 
  [1, 3, 2], 
  [4, 23, 100],
  [7, 6, 3, -2]
];

function eachNew(coll, f) {
  if (Array.isArray(coll)) {
    for (var i = 0; i < coll.length; i++) {
      f(coll[i], i);
    }
  } else {
    for (var key in coll) {
      f(coll[key], key);
    }
  }
}

function map(array, f) {
  var acc = [];
  eachNew(array, function(element, i) {
    acc.push(f(element, i));
  });
  return acc;
}
function max(array) {
    var largest = 0;
    eachNew(array, function(m){
        if (largest < m) { 
            largest = m};
    });
    return largest;
}
这看起来不是一个很好的猜测,但我一直在努力!
请帮忙,谢谢:)

你实际上就在那里,只是让事情变得有点复杂。记住,根据您的规范,map将
函数作为第二个参数。此函数将应用于作为第一个参数传递的数组。所以你只需要使用你开发的两种成分。因此,
maximum
变得相当简单:

function maximum(array) {
    return map(array, max);
}

var numbers = maximum(sampleInput);

console.log(numbers);

看起来你这样做是为了学校,所以在评论中,我想让你告诉我们为什么上面的工作。另外,您试图将
max(array)
作为映射的第二个参数,这有什么不对呢

首先确定这些函数的作用

eachNew
:获取一个对象和一个函数。如果对象是数组,则调用按顺序传递每个项和索引的函数。如果对象不是数组,则调用按顺序传递每个值和键的函数。(这些都是相似的东西,但并不完全相同。)

map
:给定一个数组和一个函数,通过使用
eachNew
对每个项目调用函数
f
并使用返回的内容构建新数组来构建新数组
f
然后是每个项目的变换,
map
变换整个数组

max
:给定数组,使用
eachNew
确定并返回数组的最大元素

您的目标是编写一个函数,将数组的元素转换为数字数组,其中每个数字都是最大值。您有一个变换函数(
map
)和一个最大值函数(
max

(,)=>

您可以根据自己的需要对其进行调整(我没有进行数据类型检查,所以您可能希望添加该检查)。我在代码中添加了注释,因此您可以遵循它。我不想给出完整的回答,只是用另一种方法来解决你的问题

'use strict';

function map (array, f) {
  for (let element_index = 0; element_index < array.length; element_index++) {
    f(array[element_index]);
  }
}

function maximum (arrays) {
  let max_values = []; /* This will hold the max values */
  for (let array_index = 0; array_index < arrays.length; array_index++) {
    const array = arrays[array_index]; /* This is the current array of values */
    /*
    let's say for now, that the max value is the first element of the array.
    (assuming it holds numbers, and at least one of them)
    */
    let max_value_of_the_array = array[0];
    map(array, value => {
      /* This function will be applied on each value of the array.
         If the value is greater than the setted before, then change it.
         If this condition is never met, then the max value is already setted.
      */
      if (value > max_value_of_the_array) {
        max_value_of_the_array = value;
      }
    })
    /* Push the max value in the max values array */
    max_values.push(max_value_of_the_array);
  }
  /* After all arrays of the array has been iterated, and all max values getted,
  return the max_values array */
  return max_values;
}

const values = [
  [1, 5, 3],
  [11, 15, 13],
  [21, 25, 23]
]

console.log(maximum(values)); // [5, 15, 25]
“严格使用”;
函数映射(数组,f){
for(让element_index=0;element_index{
/*此函数将应用于数组的每个值。
如果该值大于之前设置的值,则将其更改。
如果从未满足此条件,则已设置最大值。
*/
if(值>数组的最大值){
数组的最大值=值;
}
})
/*推送最大值数组中的最大值*/
push(数组的最大值);
}
/*迭代数组的所有数组并获取所有最大值后,
返回最大值数组*/
返回最大值;
}
常量值=[
[1, 5, 3],
[11, 15, 13],
[21, 25, 23]
]
console.log(最大值));//[5, 15, 25]

提示:请密切注意函数参数的预期类型。另外,你的浏览器控制台应该向你显示一些大错误。为什么有人投票否决了这个问题?
<transform>(<arrayOfArrays>, <functionThatTurnsAnArrayIntoAMaximum>) => <arrayOfMaximums>
'use strict';

function map (array, f) {
  for (let element_index = 0; element_index < array.length; element_index++) {
    f(array[element_index]);
  }
}

function maximum (arrays) {
  let max_values = []; /* This will hold the max values */
  for (let array_index = 0; array_index < arrays.length; array_index++) {
    const array = arrays[array_index]; /* This is the current array of values */
    /*
    let's say for now, that the max value is the first element of the array.
    (assuming it holds numbers, and at least one of them)
    */
    let max_value_of_the_array = array[0];
    map(array, value => {
      /* This function will be applied on each value of the array.
         If the value is greater than the setted before, then change it.
         If this condition is never met, then the max value is already setted.
      */
      if (value > max_value_of_the_array) {
        max_value_of_the_array = value;
      }
    })
    /* Push the max value in the max values array */
    max_values.push(max_value_of_the_array);
  }
  /* After all arrays of the array has been iterated, and all max values getted,
  return the max_values array */
  return max_values;
}

const values = [
  [1, 5, 3],
  [11, 15, 13],
  [21, 25, 23]
]

console.log(maximum(values)); // [5, 15, 25]