Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a function after a map function

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

like image 563
David Luque Avatar asked Sep 11 '15 08:09

David Luque


People also ask

Does map () method take a callback function as an argument?

The map() method returns the new array after processing each element for the given array. It accepts a callback function, which has currentItem as a mandatory parameter.

How do you call a function inside a map?

To use a map() inside a map() function in React: Call map() on the outer array, passing it a function. On each iteration, call the map() method on the other array.

How do you break a map function?

How do you break a map in react JS? If you want to map() through the last N elements of an array in React, pass a negative index to the Array.To break a map() loop in React: Call the slice() method on the array to get a portion of the array. Call the map() method on the portion of the array.

Can we break a map function?

map function? Not possible, We can't break #array. map, it will run for each element of array. To solve your problem, you can use slice first then map, slice first 5 elements of array then run map on that.


1 Answers

As soon as $scope.items is 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.

like image 75
zerkms Avatar answered Sep 17 '22 06:09

zerkms