Selecting every nth item from an array
A foreach loop provides the fastest iteration over your large array based on comparison testing. I'd stick with something similar to what you have unless somebody wishes to solve the problem with loop unrolling.
This answer should run quicker.
$result = array();
$i = 0;
foreach($source as $value) {
if ($i++ % 205 == 0) {
$result[] = $value;
}
}
I don't have time to test, but you might be able to use a variation of @haim's solution if you first numerically index the array. It's worth trying to see if you can receive any gains over my previous solution:
$result = array();
$source = array_values($source);
$count = count($source);
for($i = 0; $i < $count; $i += 205) {
$result[] = $source[$i];
}
This would largely depend on how optimized the function array_values is. It could very well perform horribly.
Javascript: take every nth Element of Array
Maybe one solution :
avoid filter because you don't want to loop over 10 000 elements !
just access them directly with a for loop !
var log = function(val){document.body.innerHTML+='<div></pre>'+val+'</pre></div>'}
var oldArr = [0,1,2,3,4,5,6,7,8,9,10]var arr = [];
var maxVal = 5;
var delta = Math.floor( oldArr.length / maxVal );
// avoid filter because you don't want// to loop over 10000 elements !// just access them directly with a for loop !// |// Vfor (i = 0; i < oldArr.length; i=i+delta) { arr.push(oldArr[i]);}
log('delta : ' + delta + ' length = ' + oldArr.length) ;log(arr);
Get every nth element of array with a function of 2 arguments
Beside the filtering solution, you could iterate and push each nth item to a new array. This approach does not visit all items, but only the nth ones.
function nthElementFinder(a, n) {
const result = [];
let i = n - 1;
while (i < a.length) {
result.push(a[i]);
i += n;
}
return result;
}
console.log(nthElementFinder([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3));
How do you select every nth item in an array?
You could also use step:
n = 2
a = ["cat", "dog", "mouse", "tiger"]
b = (n - 1).step(a.size - 1, n).map { |i| a[i] }
How to extract every n-th row using array formula
use:
=FILTER(A5:A; MOD(ROW(A5:A)+1; 3)=0)
Related Topics
Ruby: How to Convert a String to Boolean
Why Did Matz Choose to Make Strings Mutable by Default in Ruby
Using Custom To_JSON Method in Nested Objects
Why Many People Use "-%>" Instead of "%>" in Rails
Differencebetween <%= ... %> and <% ... %> in Ruby on Rails
Rvm Is Not a Function, Selecting Rubies with 'Rvm Use ...' Will Not Work
Devise Logged in Root Route Rails 3
How to Cancel Scheduled Job with Delayed_Job in Rails
What's the Best Way to Handle Exceptions from Net::Http
Add Method to an Instanced Object
How to Count the Number of Records That Have a Unique Value in a Particular Field in Ror
How to Add Migration with Multiple References to the Same Model in One Table? Ruby/Rails
Installing Jekyll on Ubuntu 14.04
Variable Scope and Order of Parsing VS. Operations: Assignment in an "If"
Ruby/Rails: Converting a Date to a Unix Timestamp
Ruby on Rails: How to Run Things in the Background
How to Install Ruby 1.9.3 in MAC Os X Mountain Lion Without Xcode