在某些条件下,在单个循环中删除Map对象元素的简洁有效方法是什么?
迭代地图的条目()可以做到但是在每次迭代时调用entries()看起来并不高效
let name_value_map = new Map([['One', 1], ['Two', 2], ['Three', 3], ['Four', 4]])
for (let pair of name_value_map.entries())
if (pair[1] <= 3)
name_value_map.delete(pair[0])
其他想法?
答案 0 :(得分:4)
使用Map.forEach()
方法:
const name_value_map = new Map([['One', 1], ['Two', 2], ['Three', 3], ['Four', 4]]);
name_value_map.forEach((v, k, m) => v <= 3 && m.delete(k));
console.log([...name_value_map.entries()]); // SO console doesn't display Map
答案 1 :(得分:3)
使用entries
和filter
以及展开运算符...
name_value_map = new Map([...name_value_map.entries()].filter( s => s[1] > 3 ));
<强>演示强>
var name_value_map = new Map([
['One', 1],
['Two', 2],
['Three', 3],
['Four', 4]
]);
name_value_map = new Map([...name_value_map.entries()].filter(s => s[1] > 3));
console.log(name_value_map); //check the browser's console
答案 2 :(得分:1)
您可以使用Array.from
,然后使用.filter
,然后转换回Map
。 这不会改变原始Map
。
Array.from()方法从中创建一个新的Array实例 类似数组或可迭代的对象。
let name_value_map = new Map([['One', 1], ['Two', 2], ['Three', 3], ['Four', 4]])
const filtered = new Map(
Array.from(name_value_map).filter(([,value]) => value > 3)
);
console.log([...filtered.entries()])
答案 3 :(得分:-1)
为什么不使用对象?
DECLARE
schemaName VARCHAR2(200) := 'Example';
dirName VARCHAR2(200) := '/dir/exampleDir';
dumpFile VARCHAR2(200) := 'TestFile.dmp';
directory VARCHAR(100) := 'EXPORT_DIR_' || schemaName;
handle NUMBER;
status VARCHAR2(20);
BEGIN
EXECUTE IMMEDIATE 'CREATE OR REPLACE DIRECTORY ' || directory || ' AS ''' || dirName || '''';
handle := DBMS_DATAPUMP.OPEN(
operation => 'EXPORT',
job_mode => 'SCHEMA',
job_name => 'TEST_EXPORT_' || schemaName);
DBMS_DATAPUMP.ADD_FILE(handle, dumpFile, directory);
DBMS_DATAPUMP.METADATA_FILTER(
handle => handle,
name => 'SCHEMA_EXPR',
value => 'IN (' || schemaName || ')');
DBMS_DATAPUMP.START_JOB(handle);
DBMS_DATAPUMP.WAIT_FOR_JOB(handle, status);
EXECUTE IMMEDIATE 'DROP DIRECTORY ' || directory;
END;