How to handle an object in recursion correctly so that you can search for min , max among value ?

object structure:

Node = { value: <number>, children: [Node] } 

function:

 const deepMap = (obj) =>{ //maxValue =(maxValue.value < obj.value) ? obj : maxValue; console.log( obj); obj.children ? obj.children.map(item => deepMap(item)) : obj; }; 

part of an object

 var graph = { value: 75, children: [{ value: 18, children: [{ value: 35, children: [{ value: 35, children: [{ value: 66, children: [{ value: 29, children: [{value: 85}] }] }] }] }] }] } 

enter image description here

  • 2
    add an example of data and result. In principle, besides the missing return code, it does what you want - Grundy
  • @Grundy here is more a question whether it is possible to process an object in the same function in order to find the maximum. Is this correct for a functional approach? - Kots Olesya
  • what do you mean to handle? - Grundy
  • @Grundy find max among the value of the screen took off the screen object - Kots Olesya
  • not an example, the example of the input date is better than text, not a picture, and not all 100–200 elements, but 3-5 so that the structure could be clearly seen, and it’s clear what the result should be - Grundy

2 answers 2

In the example code in the question there is no return , hence this function does not output anything, but it already runs through all the elements in the graph.

The first thing to change is to add return , which will return the desired value.

What is needed in this case is a maximum of several numbers: the value of the value field, and the values ​​from the children field.

By applying the deepMap function to each element of the children array, you can have a list of maximum values ​​at each level.

To find the maximum number of several, you can use the function Math.max , as well as the spread operator , which allows you to conveniently transfer an array to it.

Since the children field may be missing, you need to add the appropriate check and in the case of its execution immediately return value .

Having accepted all the notes, the function can be changed as follows:

 const deepMap = (obj) => { if (!obj.children) return obj.value; // Ссли Π½Π΅Ρ‚ Π΄Π΅Ρ‚Π΅ΠΉ - сразу Π²Π΅Ρ€Π½ΡƒΡ‚ΡŒ value var maxChildren = obj.children.map(deepMap); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹Ρ… ΠΈΠ· Π΄Π΅Ρ‚Π΅ΠΉ return Math.max(obj.value, ...maxChildren); // Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ максимальноС ΠΈΠ· Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ. }; 

If you remove local variables and if you can get the following option:

 function deepMax(obj){ return Math.max(obj.value, ...(obj.children||[]).map(deepMax)); } 

Example:

 const deepMap = (obj) => { if (!obj.children) return obj.value; // Ссли Π½Π΅Ρ‚ Π΄Π΅Ρ‚Π΅ΠΉ - сразу Π²Π΅Ρ€Π½ΡƒΡ‚ΡŒ value var maxChildren = obj.children.map(deepMap); // ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡŒΠ½Ρ‹Ρ… ΠΈΠ· Π΄Π΅Ρ‚Π΅ΠΉ return Math.max(obj.value, ...maxChildren); // Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ максимальноС ΠΈΠ· Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ. }; var graph = { value: 75, children: [{ value: 18, children: [{ value: 35, children: [{ value: 35, children: [{ value: 66, children: [{ value: 29, children: [{ value: 85 }] }] }] }] }] }] } console.log(deepMap(graph)); 

    For this structure, I would suggest this solution:

     const graph = { value: 75, children: [{ value: 18, children: [{ value: 35, children: [{ value: 35, children: [{ value: 66, children: [{ value: 29, children: [{value: 85}] }] }] }] }] }] } // Ѐункция deepMap рСкурсивно ΠΎΠ±Ρ…ΠΎΠ΄ΠΈΡ‚ Π΄Π΅Ρ€Π΅Π²ΠΎ obj // Π’Ρ‚ΠΎΡ€ΠΎΠΉ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ func - нСобходимая функция сравнСния function deepMap(obj, func){ if(obj.children){ // Если Π΅ΡΡ‚ΡŒ ΠΏΠΎΡ‚ΠΎΠΌΠΊΠΈ - Ρ‚ΠΎ ΡƒΡ…ΠΎΠ΄ΠΈΠΌ Π² Ρ€Π΅ΠΊΡƒΡ€ΡΠΈΡŽ let res = obj.value; // РСкурсивно ΠΏΡ€ΠΎΡ…ΠΎΠ΄ΠΈΠΌ ΠΏΠΎ всСм ΠΏΠΎΡ‚ΠΎΠΌΠΊΠ°ΠΌ ΠΈ вычисляСм Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ for(let i = 0; i < obj.children.length; i++) { res = func(res, deepMap(obj.children[i], func)); } return res; } else { // НСт ΠΏΠΎΡ‚ΠΎΠΌΠΊΠΎΠ² - Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ return obj.value } } console.log(`Max result for graph is: ${deepMap(graph, Math.max)}`); console.log(`Min result for graph is: ${deepMap(graph, Math.min)}`);