Mutual conversion between Map, Set, Array and Object in JS

Native js can support the conversion between these types of data formats, first understand the role of the two native methods Object.entries and Object.FromEntries:

Object.entries obtains the key-value pair of the object
Object.FromEntries converts the key-value pair list into the object
Object.entries and Object.fromEntries are reversible.

Object to Map

let obj={foo:'hello',bar:100};
let map=new Map(Object.entries(obj));
console.log(map)

Map to Object

let map=new Map([['foo','hello'],['bar',100]]);
let obj=Object.fromEntries(map);
console.log(obj);

Object to Array

let obj={'foo':'hello','bar':100};
let arr=Object.entries(obj);
console.log(arr);

Convert Array to Object

let arr=[['foo','hello'],['bar',100]];
let obj=Object.fromEntries(arr);
console.log(obj);

Array to Set

let arr=[['foo','hello'],['bar',100]];
let set=new Set(arr);
console.log(set)

Guess you like

Origin blog.csdn.net/lianjiuxiao/article/details/114549112