Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!