skip to Main Content

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


  1. As colleagues rightly mentioned in the comments, you need to leverage three things:

    1. Create an array or map (in this version of the implementation) to keep track only of the delay values
    2. Map is used to keep serialized coordinates as keys (x,y,z) and
      values are corresponding data objects. It will allow you to avoid
      nested arrays and use the capabilities of Map to make an
      efficient lookup.
    3. Leverage Math.max function to find the maximum in an subset

    InitializeMap function:

    Creates a transformed set with key as coordinates string and corresponding value.

    function InitializeMap() {
        const entries = [
            { x: 127, y: 1, z: 0, data: {name:"Gl", ss:["Z0",127,"B0",1,[58,0]], delay:{first:60, term:220, slow:240, med:188, fast:188}}},
            { x: 127, y: 127, z: 0, data: {name:"Gl Co", ss:["Z0",127,"B0",127,[58,0]], delay:{first:60, term:220, slow:240, med:188, fast:188}}},
            { x: 127, y: 1, z: 6, data: {name:"Gl El", ss:["Z0",127,"B0",1,[58,6]], delay:{first:60, term:310, slow:360, med:245, fast:145}}},
            { x: 127, y: 127, z: 6, data: {name:"Gl El Co", ss:["Z0",127,"B0",127,[58,6]], delay:{first:60, term:310, slow:330, med:245, fast:145}}}
        ];
    
        entries.forEach(entry => {
            const key = `${entry.x},${entry.y},${entry.z}`;
            ARR_Map.set(key, entry.data.delay); // Store only delay objects
        });
    }
    

    Search Max Value

    Iterate over the transformed map and search the max value within each set. Then find the largest value of all.

    // Function to find the maximum delay using Map
    function findMaxDelayUsingMap(arrMap) {
        let maxDelay = -Infinity;
        for (let delayObj of arrMap.values()) {
            const localMax = Math.max(...Object.values(delayObj).filter(val => typeof val === 'number'));
            if (localMax > maxDelay) {
                maxDelay = localMax;
            }
        }
        return maxDelay;
    }
    

    Complete code example:

    // Initialize a Map to store only initialized entries
    const ARR_Map = new Map();
    
    // Modified Initialize function to use Map
    function InitializeMap() {
        const entries = [
            { x: 127, y: 1, z: 0, data: {name:"Gl", ss:["Z0",127,"B0",1,[58,0]], delay:{first:60, term:220, slow:240, med:188, fast:188}}},
            { x: 127, y: 127, z: 0, data: {name:"Gl Co", ss:["Z0",127,"B0",127,[58,0]], delay:{first:60, term:220, slow:240, med:188, fast:188}}},
            { x: 127, y: 1, z: 6, data: {name:"Gl El", ss:["Z0",127,"B0",1,[58,6]], delay:{first:60, term:310, slow:360, med:245, fast:145}}},
            { x: 127, y: 127, z: 6, data: {name:"Gl El Co", ss:["Z0",127,"B0",127,[58,6]], delay:{first:60, term:310, slow:330, med:245, fast:145}}}
        ];
    
        entries.forEach(entry => {
            const key = `${entry.x},${entry.y},${entry.z}`;
            ARR_Map.set(key, entry.data.delay); // Store only delay objects
        });
    }
     
    InitializeMap();
    
    // Function to find the maximum delay using Map
    function findMaxDelayUsingMap(arrMap) {
        let maxDelay = -Infinity;
        for (let delayObj of arrMap.values()) {
            const localMax = Math.max(...Object.values(delayObj).filter(val => typeof val === 'number'));
            if (localMax > maxDelay) {
                maxDelay = localMax;
            }
        }
        return maxDelay;
    }
    
    let maximumDelayMap = findMaxDelayUsingMap(ARR_Map);
    console.log("The maximum delay using Map is:", maximumDelayMap);
    
    Login or Signup to reply.
    • You need to compare the existing maxValue with the maximum from the current .delay object.
    • You should initialize all the elements to objects, not arrays.
    • You need to check if the current element has a .delay property.
    • You had an extra nested loop (for let l ...) that wasn’t needed. You never used l.
    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++) {
          if (ARR[i][j][k].delay) {
            maxValue = Math.max(maxValue, ...Object.values(ARR[i][j][k].delay));
          }
        }
      }
    }
    
    console.log(maxValue);
    Login or Signup to reply.
  2. 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 a max value from local ones (as in previous answer).

    const ARR_SIZE = 128;
    
    // Initialize the ARR 3D array with nulls for uninitialized entries
    const ARR = Array.from({ length: ARR_SIZE }, () =>
        Array.from({ length: ARR_SIZE }, () =>
            Array.from({ length: ARR_SIZE }, () => null)
        )
    );
    
    
    let initializedEntriesFunctional = [];
    
    function InitializeFunctional() {
        const entries = [
            {
                x: 127, y: 1, z: 0,
                data: { name: "Gl", ss: ["Z0", 127, "B0", 1, [58, 0]], delay:{ first: 60, term: 220, slow: 240, med: 188, fast: 188 }
            }
        ]
    
        entries.forEach(entry => {
            // Populate the ARR array with the data object
            ARR[entry.x][entry.y][entry.z] = entry.data;
    
            // Track only the delay objects for efficient processing
            initializedEntriesFunctional.push(entry.data.delay);
        });
    }
    
    // Execute the initialization
    InitializeFunctional();
    
    
    function findMaxDelayFunctional(initializedDelays) {
        // Extract all numerical delay values and flatten them into a single array
        const allDelays = initializedDelays.flatMap(delayObj =>
            Object.values(delayObj).filter(val => typeof val === 'number')
        );
    
        // Use Math.max to find the largest delay value
        return Math.max(...allDelays);
    }
    
    const maximumDelayFunctional = findMaxDelayFunctional(initializedEntriesFunctional);
    console.log("The maximum delay using Functional Programming is:", maximumDelayFunctional);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search