使用地图,任意参数在Javascript中重建_zip [英] Rebuilding _zip in Javascript using map, arbitrary arguments

查看:62
本文介绍了使用地图,任意参数在Javascript中重建_zip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试很好地学习JavaScript,并正在尝试重建一些下划线功能.我正在尝试使用map重建zip,其中存在任意数量的参数.这是我想出的解决方案.我知道下划线实现本身使用_pluck,而内部使用Map,因此我想看看是否有可能使用map ...

I'm trying to learn JavaScript well and am practicing rebuilding some underscore functions. I'm trying to rebuild zip using map where there is an arbitrary number of arguments. Here is the solution I came up with, with pluck. I know that the underscore implementation itself uses _pluck, which internally uses Map, so I wanted to see if it was possible to do this with map...

_.zip = function() {

     var argumentsArray = Array.prototype.slice.call(arguments);
     var longestArray = argumentsArray.sort(function(a, b) {
         return b.length - a.length
     })[0];

     //create and return an array that is as long as the longestArray:

     var zipped = Array(longestArray.length);
     // you want to push each element from each array onto an array with the length of the longestArray.

     for (var i = 0; i < longestArray.length; i++) {
         zipped[i] = _.pluck(argumentsArray, i)
     };
     return zipped;
   }

我在下面的map函数中停留在返回值上.我知道我必须对最长元素的长度进行某种循环,因为返回的数组应该那么长.我将如何在地图内执行此操作?或者,我应该只做两个for循环,而不是尝试使用map?

I'm stuck on what to return inside the map function below. I know I have to do some sort of a loop up to the length of the longest element, since the returned array should be that long. How would I do that inside map? Or, should I just do two for loops instead of trying to use map?

 zip = function() {

      //get the longest input argument:
      var argumentsArray = Array.prototype.slice.call(arguments);
      var longestArray = argumentsArray.sort(function(a, b) {
          return b.length - a.length
      })[0];


    //trying to use map here:

    return map(argumentsArray, function(val){
      return ?
    })

  };

  console.log(zip([1, 2, 4], [1])) 
// returns [[1, 1],[2, undefined],[4, undefined]]

推荐答案

下面我附上了原始实现的工作副本,而不使用仅使用映射的pluck.

Below I have attached what should be a working copy of your original implementation without the use of pluck using only maps.

var zip = function() {
  var argumentsArray = Array.prototype.slice.call(arguments);
  var longestArray = argumentsArray.sort(function(a, b) {
    return b.length - a.length
  })[0];

  return longestArray.map(function(value, index, array) {
    return argumentsArray.map(function(val, i, arr) {
      return val[index];
    });
  });
};

longestArray上的外部映射仅充当循环机制,因此更适合使用for循环.

The outer map over longestArray acts solely as a looping mechanism so it would be better suited to use a for-loop instead.

内部循环映射传入的参数数组,并使用外部映射的当前索引返回每个参数数组的第ith个元素.由于map已经返回了一个新数组,因此内部map的每次迭代都会返回一个包含每个参数数组的ith个元素的数组.

The inner loop maps over the array of arguments passed in and, using the current index of the outer map, returns the ith element of each argument array. Since map already returns a new array, each iteration of the inner map will return an array containing the ith elements for each argument array.

下面是使用for循环和map的另一种实现.

Below is another implementation using a for loop and a map.

function zip() {
  //turn args into an array
  var argumentsArray = Array.prototype.slice.call(arguments);
  var returnArr = [];

  //get length of longest array
  var length = argumentsArray.reduce(function (prev, curr) {
    //starter val is 0, if curr array is longer replace with its length
    return (prev >= curr.length) ? prev : curr.length;
  }, 0);

  //push an array of the ith element of each of the argument arrays
  //into the return array
  for (var i = 0; i < length; i++) {
    returnArr.push(argumentsArray.map(function (val) {
      return val[i];
    }));
  }

  return returnArr;
}

还要注意,我不进行排序来查找最大的数组,而是减少了数组的长度以查找并返回最长的数组.

Also note that instead of sorting to find the largest array, I reduce over the array lengths to find and return the longest length.

由于js排序就位,因此有可能更改参数数组的顺序.然后,您返回的压缩元素数组将按照其原始数组长度进行排序.这样,它们将按照传递参数数组的顺序排列.但是,这两种都是有效的zip实现,具体取决于您的需求.

Since js sort is in-place it will potentially change your arguments array order. Your returned array of zipped elements will then be ordered by their original array lengths. Doing it this way they will instead be in the order that the argument arrays were passed in. But both are valid zip implementations depending on what you need.

希望这会有所帮助!

这篇关于使用地图,任意参数在Javascript中重建_zip的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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