javascript 在 map 函数之后执行一个函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32519068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:24:03  来源:igfitidea点击:

Execute a function after a map function

javascriptangularjsasynchronous

提问by David Luque

I have this code

我有这个代码

$scope.items.map(function (item) {
   if(item.keywords.indexOf($scope.formData.keyword) != -1){
       array.push(bono);
   }
})

And I need to execute a function with all the elements of the array when the map finish. How can I do this? I thought to stack the calls but I don't know how to do it.

当地图完成时,我需要使用数组的所有元素执行一个函数。我怎样才能做到这一点?我想堆叠调用,但我不知道该怎么做。

Thanks

谢谢

回答by zerkms

As soon as $scope.itemsis an array as you stated in the question and the Array.prototype.map()is synchronous - which means that you simply put the next statement after this code and it will be executed after the .map()has completed processing.

只要$scope.items是您在问题中所述的数组并且Array.prototype.map()是同步的 - 这意味着您只需将下一个语句放在此代码之后,它将在.map()完成处理后执行。

回答by Cristian Smocot

try this:

试试这个:

var array = [];
$scope.items.map(function (item) {
   if(item.keywords.indexOf($scope.formData.keyword) != -1){
       array.push(item);
   }
});

myFunction(array);

Array.map()documentation:

Array.map()文档:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

回答by Nageshwar Reddy

try like this

像这样尝试

$scope.items.map(function (item) {
   if(item.keywords.indexOf($scope.formData.keyword) != -1){
       array.push(bono);
   }
}).then(function(){}).then(function(){});