在 Javascript 中,如何同时对两个 seq 的元素执行函数?

发布于 2025-01-02 06:19:16 字数 330 浏览 5 评论 0原文

在 Javascript 中,同时迭代两个数组并在两个运行元素上调用函数的最佳方法是什么?例如:

var a = [1, 2, 3];
var b = [11, 12, 13];
function my_func() { /* some custom function */}

代码应该执行 my_func 3 次,如下所示:

my_func(1,11);
my_func(2,12);
my_func(3,13);

如何在不定义新函数但使用 jQuery/underscore api 的情况下实现这一目标?

In Javascript, what is the best way for iterating concurrently on two arrays and calling a function on the two running elements? For example:

var a = [1, 2, 3];
var b = [11, 12, 13];
function my_func() { /* some custom function */}

The code should execute my_func 3 times, like this:

my_func(1,11);
my_func(2,12);
my_func(3,13);

How can I achieve that without defining a new function, but using jQuery/underscore api?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘虞初梦 2025-01-09 06:19:16

例如,这样(使用underscore.js)?

_.each( _.zip(a,b), my_func );

function my_func( pair ){
  alert( pair[0] + pair[1] );
}

或者:

_.each( _.zip(a,b), function( pair ){
  my_func( pair[0], pair[1] );
  // or: my_func.apply( null, pair );
});

function my_func( a, b ){
  alert( a + b );
}

-

function wrapply( func, thisObj ){
  return function( args ){
    return func.apply( thisObj, args );
  }
}

var add = wrapply( function(a,b){ return a+b; });
alert( add([1,2]) );

For example, this way (using underscore.js)?

_.each( _.zip(a,b), my_func );

function my_func( pair ){
  alert( pair[0] + pair[1] );
}

Or:

_.each( _.zip(a,b), function( pair ){
  my_func( pair[0], pair[1] );
  // or: my_func.apply( null, pair );
});

function my_func( a, b ){
  alert( a + b );
}

-

function wrapply( func, thisObj ){
  return function( args ){
    return func.apply( thisObj, args );
  }
}

var add = wrapply( function(a,b){ return a+b; });
alert( add([1,2]) );
地狱即天堂 2025-01-09 06:19:16

嗯,直接的 JavaScript 非常擅长做到这一点。只需使用标准的 for 循环,假设您的 a 数组和 b 数组具有相同的长度。

for (var i = 0; i < a.length; i++) {
    my_func(a[i], b[i]);
}

jQuery 还提供了 .each() 方法,可以非常轻松地对一个序列进行操作。对两个进行操作只需要多一点代码。

$.each(a, function(i, aItem) { my_func(aItem, b[i]); });

如果您想使用 zip 操作,则可以使用 jQueryArrayUtils 插件

$.zip(a, b).each(function() {
    var aItem = this[0];
    var bItem = this[1];
    my_func(aItem, bItem);
});

作为 Python 的粉丝,最后一种方法对我来说似乎特别好。但老实说,如果您不打算做任何比调用 my_func 更复杂的事情,那么您也可以使用普通的 for 循环。

Well, straight-forward JavaScript is pretty good at doing this. Just use a standard for loop, assuming that your a arrays and b array have the same length.

for (var i = 0; i < a.length; i++) {
    my_func(a[i], b[i]);
}

jQuery also provides an .each() method that can make operating on one sequence very easy. Operating on two just takes a bit more code.

$.each(a, function(i, aItem) { my_func(aItem, b[i]); });

And if you want to use a zip operation, you can use the jQueryArrayUtils plugin.

$.zip(a, b).each(function() {
    var aItem = this[0];
    var bItem = this[1];
    my_func(aItem, bItem);
});

Being a fan of Python, this last method seems particularly nice to me. But honestly, if you're not going to do anything more complicated than calling my_func, then you may as well just use a normal for loop.

林空鹿饮溪 2025-01-09 06:19:16
[1,2,3].forEach(function(e, i, arr){my_func(e, arr[i]);}, [11,12,13]);

jQuery 版本:

$.each(a, function(index, value) { 
    my_func(value, b[index]);
});
[1,2,3].forEach(function(e, i, arr){my_func(e, arr[i]);}, [11,12,13]);

jQuery version:

$.each(a, function(index, value) { 
    my_func(value, b[index]);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文