Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is here simple way to convert Set into Array? [duplicate]

I tried:

  const state =  new Set( [5, 10, 15, 20, 30, 45, 60] );
  let preset = Object.assign( {}, state );
  console.log(preset)
  // {}

and got empty object. Also I tried to convert state to string and also failed:

state.values().toLocaleString()
// "[object Set Iterator]"

Or the only way is to iterate Set one by one?

Update: It should be an Array of values: [5, 10, 15, 20, 30, 45, 60] as a result.

like image 604
Max Kurtz Avatar asked Jan 01 '23 10:01

Max Kurtz


1 Answers

Just use Array.from method or spread syntax.

const state =  new Set( [5, 10, 15, 20, 30, 45, 60] );
console.log(Array.from(state));
console.log([...state]);
like image 146
Mihai Alexandru-Ionut Avatar answered Jan 04 '23 05:01

Mihai Alexandru-Ionut