Here is the current code and a section of the larger array:
function Initialize() {
ARR[127][1][0] = {name:"Gl", ss:["Z0",127,"B0",1,[58,0]], delay:{first:60, term:220, slow:240, med:188, fast:188}};
ARR[127][127][0] = {name:"Gl Co", ss:["Z0",127,"B0",127,[58,0]], delay:{first:60, term:220, slow:240, med:188, fast:188}};
ARR[127][1][6] = {name:"Gl El", ss:["Z0",127,"B0",1,[58,6]], delay:{first:60, term:310, slow:360, med:245, fast:145}};
ARR[127][127][6] = {name:"Gl El Co", ss:["Z0",127,"B0",127,[58,6]], delay:{first:60, term:310, slow:330, med:245, fast:145}}
}
var ARR = new Array(128);
for (let val1 = 0; val1 < ARR.length; val1++) {
ARR[val1] = new Array(128);
for (let val2 = 0; val2 < ARR[val1].length; val2++) {
ARR[val1][val2] = new Array(128);
for (let val3 = 0; val3 < ARR[val1][val2].length; val3++) {
ARR[val1][val2][val3] = [];
}
}
}
Initialize();
var maxValue = 0;
for (let i = 0; i < ARR.length; i++) {
for (let j = 0; j < ARR[i].length; j++) {
for (let k = 0; k < ARR[i][j].length; k++) {
for (let l = 0; l < ARR[i][j][k].length; l++) {
maxValue = Math.max(...Object.values(ARR[i][j][k].delay))
}
}
}
}
console.log(maxValue);
I’m looking to find the Largest value in the .delay
object – from ALL lines. In this case it’s 360.
I have looked at different methods but they all seem to be for object arrays with the same object, but each object in .delay
has a different name.
3
Answers
As colleagues rightly mentioned in the comments, you need to leverage three things:
values are corresponding data objects. It will allow you to avoid
nested arrays and use the capabilities of Map to make an
efficient lookup.
InitializeMap function:
Creates a transformed set with
key
as coordinates string and corresponding value.Search Max Value
Iterate over the transformed map and search the max value within each set. Then find the largest value of all.
Complete code example:
maxValue
with the maximum from the current.delay
object..delay
property.(for let l ...)
that wasn’t needed. You never usedl
.Actually, after giving it a good thought, it can be implemented even in a simpler manner compared to previous answer. The difference here is that we can leverage array and
flatMap()
to avoid another lookup of amax
value from local ones (as in previous answer).