다음이 우리의 세트라고 가정해 봅시다 -
var name = new Set(['John', 'David', 'Bob', 'Mike']);
세트를 객체로 변환하려면 JavaScript에서 Object.assign()을 사용하십시오 -
var setToObject = Object.assign({}, ...Array.from(name, value => ({ [value]: 'not assigned' }))); 예시
다음은 코드입니다 -
var name = new Set(['John', 'David', 'Bob', 'Mike']);
var setToObject = Object.assign({}, ...Array.from(name, value => ({ [value]: 'not assigned' })));
console.log("The Set result=");
console.log(name);
console.log("The Object result=");
console.log(setToObject); 위의 프로그램을 실행하려면 다음 명령을 사용해야 합니다 -
node fileName.js.
여기에서 내 파일 이름은 demo260.js입니다.
출력
이것은 콘솔에 다음과 같은 출력을 생성합니다 -
PS C:\Users\Amit\javascript-code> node demo260.js
The Set result=
Set { 'John', 'David', 'Bob', 'Mike' }
The Object result=
{
John: 'not assigned',
David: 'not assigned',
Bob: 'not assigned',
Mike: 'not assigned'
}