Suppose I have an array like such:
var test_array = [0.1, 2.1, 0.7, 5.4, 3.2, 1.6];
I basically want to grab the first 2 values in the array, place them into 2 separate variables and then move on to the next step, like so:
var test_array = [0.1, 2.1, 0.7, 5.4, 3.2, 1.6];
test_array.sort();
for (let i=0;i< test_array.length -1; i++) {
var j = i + 1;
var pt_one = test_array[i];
var pt_two = test_array[j];
console.log("pt_one = " + pt_one);
console.log("pt_two = " + pt_two);
}
Is there a more efficient way to do this? Could the forEach() method be used somehow?
forEachmight be a bit better, but not by much. Your current code is fine.jvariable, just writetest_array[i+1]