ES6 Set & Map

时间:2022-06-17 14:30:32

ES6 Set & Map

OK

ES6 Map

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map


const arr = [
    "INNERCODE", "SECUCODE", "SECUABBR", "TRADINGDAY", "LASTWEEKTRADINGDAY", "CLOSEPRICE", "CHANGE", "CHANGERANGE",
    "INNERCODE", "SECUCODE", "SECUABBR", "TRADINGDAY", "LASTWEEKTRADINGDAY", "CLOSEPRICE", "CHANGE", "CHANGERANGE",
];

let map_arr = new Map(arr);
// VM17757:1 Uncaught TypeError: Iterator value INNERCODE is not an entry object at new Map (<anonymous>)

let set_arr = new Set(arr);
// Set(8) {"INNERCODE", "SECUCODE", "SECUABBR", "TRADINGDAY", "LASTWEEKTRADINGDAY", …}

let new_arr = [...set_arr];
// (8) ["INNERCODE", "SECUCODE", "SECUABBR", "TRADINGDAY", "LASTWEEKTRADINGDAY", "CLOSEPRICE", "CHANGE", "CHANGERANGE"]

ES6 Set

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

let mySet = new Set();

mySet.add(1);
// Set [ 1 ]

mySet.add(5);
// Set [ 1, 5 ]

mySet.add(5);
 // Set [ 1, 5 ]

mySet.add('some text');
// Set [ 1, 5, 'some text' ]