搜索一个阵列,其中包括子阵列的最大数量和收益的新阵列 [英] Search An Array Consisting of Sub-Arrays For the Largest Number and Return in a New Array

查看:190
本文介绍了搜索一个阵列,其中包括子阵列的最大数量和收益的新阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的编码挑战,采取定数组它由子阵列,搜索每个子阵列中数量最多,最后返回只由最大数的新数组。我的想法过程是创造出来的变量每个子阵列的,写一个for循环的每个值在阵列内比较,那么最大的价值推到一个新的数组。写我的第一个for循环后,我测试了我的code,看到我收到整个第一子阵的一个意想不到的结果被推到我新的数组。我找了这个错误之前,我写在未来三环路。谢谢。编辑:这是初学者的JavaScript codeRS和建议表示您的解决方案使用比较运算符

 函数largestOfFour(ARR){
      变种1 =改编[0];
      变种2 =改编[1];
      变种3 = ARR [2];
      变种4 =改编[3];
      变种newArr = [];      对于(VAR I = 0; I< one.length;我++){
        变种oneLrg = 0;
        如果(一个[I]≥oneLrg){
          oneLrg =一个[我]
          }
        newArr.push(oneLrg);
      }  返回ARR;
}的console.log(largestOfFour([4,5,1,3],[13,27,18,26],[32,35,37,39],[1000,1001,857,1])); //这个测​​试的情况下返回[4,5,1,3],而不仅仅是[5]


解决方案

使用<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators#Greater_than_operator_%28%3E%29\"相对=nofollow> &GT;

VAR newArr = [];
对于(VAR I = 0; I&LT; arr.length ++ I){//迭代数组
  VAR最大=负无穷大; //初始最大
  对于(VAR J = 0; J&LT;常用3 [I]。长度; ++ j)条//迭代子阵
    如果(ARR [I] [J]&GT;最大值)//比较
      最大= ARR [I] [J]。 //更新最大
  newArr.push(最大); //存放实际最大
}

使用 数学的.max

VAR newArr = [];
对于(VAR I = 0; I&LT; arr.length ++ I){//迭代数组
  VAR最大=负无穷大; //初始最大
  对于(VAR J = 0; J&LT;常用3 [I]。长度; ++ j)条//迭代子阵
    最大= Math.max(最大,编曲[I] [J]); //更新最大
  newArr.push(最大); //存放实际最大
}

添加<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply\"相对=nofollow> 适用

VAR newArr = [];
对于(VAR I = 0; I&LT; arr.length ++ I)//迭代数组
  newArr.push(//商店...
    Math.max.apply(数学,编曲由[i])// ...最大的子数组
  );

添加的ECMAScript 5 地图

VAR newArr = arr.map(功能(子数组){
  返回Math.max.apply(数学,子阵);
});

添加的ECMAScript 5 <一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind\"相对=nofollow>绑定,

VAR newArr = arr.map(Function.apply.bind(Math.max,数学));

或添加的ECMAScript 6 箭头功能 US $ p $垫运营商

VAR newArr = arr.map(子数组=&GT; Math.max(...子阵));

I am working on a coding challenge to take a given array which consists of sub-arrays, search for the largest number in each sub-array, and finally return a new array consisting only of the largest numbers. My thought process was to create variables out of each subarray, write a for-loop comparing each value within the array, then push the largest value to a new array. After writing my first for-loop I tested my code and see that I am getting an unexpected result of the entire first subarray being pushed into my new array. I am looking for the mistake before I write the next three loops. Thank you. Edit: This is for beginner JavaScript coders and the suggestion indicates to use comparison operators in your solution.

function largestOfFour(arr) {
      var one = arr[0];
      var two = arr[1];
      var three = arr[2];
      var four = arr[3];
      var newArr = [];

      for (var i = 0; i < one.length; i++){
        var oneLrg = 0;
        if (one[i] > oneLrg){
          oneLrg = one[i];
          }
        newArr.push(oneLrg);
      }  

  return arr;
}

console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]])); //This test case returns [4,5,1,3] instead of just [5]

解决方案

Using >:

var newArr = [];
for(var i=0; i<arr.length; ++i) {           // Iterate array
  var maximum = -Infinity;                  // Initial maximum
  for(var j=0; j<arr[i].length; ++j)        // Iterate subarrays
    if(arr[i][j] > maximum)                 // Compare
      maximum = arr[i][j];                  // Update maximum
  newArr.push(maximum);                     // Store the real maximum
}

Using Math.max:

var newArr = [];
for(var i=0; i<arr.length; ++i) {           // Iterate array
  var maximum = -Infinity;                  // Initial maximum
  for(var j=0; j<arr[i].length; ++j)        // Iterate subarrays
    maximum = Math.max(maximum, arr[i][j]); // Update maximum
  newArr.push(maximum);                     // Store the real maximum
}

Adding apply:

var newArr = [];
for(var i=0; i<arr.length; ++i)     // Iterate array
  newArr.push(                      // Store ...
    Math.max.apply(Math, arr[i])    // ... the maximum of the subarray
  );

Adding ECMAScript 5 map,

var newArr = arr.map(function(subarray) {
  return Math.max.apply(Math, subarray);
});

Adding ECMAScript 5 bind,

var newArr = arr.map(Function.apply.bind(Math.max, Math));

Or adding ECMAScript 6 arrow functions and spread operator,

var newArr = arr.map(subarray => Math.max(...subarray));

这篇关于搜索一个阵列,其中包括子阵列的最大数量和收益的新阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆