3.8 KiB
3.8 KiB
id, challengeType, videoUrl, title
id | challengeType | videoUrl | title |
---|---|---|---|
8d5823c8c441eddfaeb5bdef | 1 | 创建地图数据结构 |
Description
Map
对象作为JavaScript object
的包装器。在Map对象上创建以下方法和操作: -
add
接受要添加到地图的key, value
对。 -
remove
接受一个键并删除关联的key, value
对 -
get
接受一个key
并返回存储的value
- 如果密钥存在
has
接受key
并返回true,否则返回false 。 -
values
返回地图中所有values
的数组 -
size
返回地图中的项目数 -
clear
清空地图
Instructions
Tests
tests:
- text: 存在地图数据结构。
testString: assert((function() { var test = false; if (typeof Map !== 'undefined') { test = new Map() }; return (typeof test == 'object')})());
- text: Map对象具有以下方法:add,remove,get,has,values,clear和size。
testString: "assert((function() { var test = false; if (typeof Map !== 'undefined') { test = new Map() }; return (typeof test.add == 'function' && typeof test.remove == 'function' && typeof test.get == 'function' && typeof test.has == 'function' && typeof test.values == 'function' && typeof test.clear == 'function' && typeof test.size == 'function')})());"
- text: add方法将项添加到地图中。
testString: assert((function() { var test = false; if (typeof Map !== 'undefined') { test = new Map() }; test.add(5,6); test.add(2,3); test.add(2,5); return (test.size() == 2)})());
- text: has方法对于添加的项返回true,对于缺少的项返回false。
testString: assert((function() { var test = false; if (typeof Map !== 'undefined') { test = new Map() }; test.add('test','value'); return (test.has('test') && !test.has('false'))})());
- text: get方法接受键作为输入并返回关联的值。
testString: assert((function() { var test = false; if (typeof Map !== 'undefined') { test = new Map() }; test.add('abc','def'); return (test.get('abc') == 'def')})());
- text: values方法将存储在映射中的所有值作为数组中的字符串返回。
testString: assert((function() { var test = false; if (typeof Map !== 'undefined') { test = new Map() }; test.add('a','b'); test.add('c','d'); test.add('e','f'); var vals = test.values(); return (vals.indexOf('b') != -1 && vals.indexOf('d') != -1 && vals.indexOf('f') != -1)})());
- text: clear方法清空映射,size方法返回映射中存在的项目数。
testString: assert((function() { var test = false; if (typeof Map !== 'undefined') { test = new Map() }; test.add('b','b'); test.add('c','d'); test.remove('asdfas'); var init = test.size(); test.clear(); return (init == 2 && test.size() == 0)})());
Challenge Seed
var Map = function() {
this.collection = {};
// change code below this line
// change code above this line
};
Solution
// solution required
/section>