Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
Function returns [object Set] instead of an actual Set

I have a function

function recursiveDivisionWrap(width, height, colStart, colEnd, rowStart, rowEnd)

in which i have an empty set

let blockedNodes = new Set();

inside this function I have another, recursive function

function recursiveDivision(width, height, colStart, colEnd, rowStart, rowEnd)

On each call of the recursiveDivision function i update a blockedNodes set by adding value:

for (let i = colStart; i < colEnd; i++) {
        blockedNodes.add(`c${i}r${wallStart}`);
      }

If I console.log blockedNodes set inside recursiveDivision function, I'm getting desired result, for example:

Set(43) {'c0r7', 'c1r7', 'c2r7', 'c3r7', 'c4r7', …}

However when I console.log blockedNodes set inside recursiveDivisionWrap and not in recursiveDivision, I'll get comma separated object:

c0r7,c1r7,c2r7,c3r7,c4r7,c5r7,c6r7,c7r7,c8r7,c9r7,c9r0,c9r1,c9r2,c9r3,c9r4,c9r5,c9r6,c8r0,c8r1,c8r2,c8r3,c8r4,c8r5,c8r6,c3r0,c3r1,c3r2,c3r3,c3r4,c3r5,c3r6,c4r6,c5r6,c6r6,c7r6,c4r1,c5r1,c6r1,c7r1,c4r5,c5r5,c6r5,c7r5

I've also tried with array and the result was the same.

Why doesn't it return Set(43) {'c0r7', 'c1r7', 'c2r7', 'c3r7', 'c4r7', …} if the blockedNodes set is defined outside the recursiveDivision function and inside recursiveDivisionWrap and why does the inner recursiveDivision function returns correct set? I would appreciate any help in finding answer to that question.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

This behaviour has nothing to do with the place where the output is made, but how the output is formatted.

You have these statements:

console.log(blockedNodes);

And:

console.log(`Is this a set? ${blockedNodes}`);

And:

let ecie = Array.from(blockedNodes);
console.log(`Is this an array? ${ecie}`)

They don't do the same thing.

console.log(blockedNodes) leaves the rendering to the console API, which may provide additional information, like the name of the constructor of the object (Set in this case), the number of items this collection has, ...etc. It may even add user-interface controls to expand/collapse the contents of the collection and potential nested data structures.

console.log(`Is this a set? ${blockedNodes}`) will implicitly call blockedNodes.toString() in order to produce the string. Unless toString has been overriden, that will render as [object Set], and so the total output will be "Is this a set? [object Set]".

console.log(`Is this an array? ${ecie}`) will also call the toString method, but this time on ecie, which is an array (as that is what Array.from returned). The toString method on arrays produces a comma separated list of the values, which explains the output you mentioned.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda