更新时间:2016年11月30日16时26分 来源:传智播客web前端开发培训学院 浏览次数:
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
function getCollectionWeight(collection) {let collectionValues;if (collection instanceof Array) {collectionValues = collection;} else if (collection instanceof Map) {collectionValues = [...collection.values()];} else {collectionValues = Object.keys(collection).map(function (key) {return collection[key];});}return collectionValues.reduce(function(sum, item) {if (item == null) {return sum + 1;}if (typeof item === 'object' || typeof item === 'function') {return sum + 4;}return sum + 2;}, 0);}let myArray = [null, { }, 15];let myMap = new Map([ ['functionKey', function() {}] ]);let myObject = { 'stringKey': 'Hello world' };getCollectionWeight(myArray);// => 7 (1 + 4 + 2)getCollectionWeight(myMap);// => 4getCollectionWeight(myObject); // => 2 |
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
// Code extracted into getWeightByType()function getWeightByType(value) {const WEIGHT_NULL_UNDEFINED= 1;const WEIGHT_PRIMITIVE = 2;const WEIGHT_OBJECT_FUNCTION = 4;if (value == null) {return WEIGHT_NULL_UNDEFINED;}if (typeof value === 'object' || typeof value === 'function') {return WEIGHT_OBJECT_FUNCTION;}return WEIGHT_PRIMITIVE;}function getCollectionWeight(collection) {let collectionValues;if (collection instanceof Array) {collectionValues = collection;} else if (collection instanceof Map) {collectionValues = [...collection.values()];} else {collectionValues = Object.keys(collection).map(function (key) {return collection[key];});}return collectionValues.reduce(function(sum, item) {return sum + getWeightByType(item);}, 0);}let myArray = [null, { }, 15];let myMap = new Map([ ['functionKey', function() {}] ]);let myObject = { 'stringKey': 'Hello world' };getCollectionWeight(myArray);// => 7 (1 + 4 + 2)getCollectionWeight(myMap);// => 4getCollectionWeight(myObject); // => 2 |
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
function getWeightByType(value) {const WEIGHT_NULL_UNDEFINED = 1;const WEIGHT_PRIMITIVE = 2;const WEIGHT_OBJECT_FUNCTION = 4;if (value == null) {return WEIGHT_NULL_UNDEFINED;}if (typeof value === 'object' || typeof value === 'function') {return WEIGHT_OBJECT_FUNCTION;}return WEIGHT_PRIMITIVE;}// Code extracted into getMapValues()function getMapValues(map) {return [...map.values()];}// Code extracted into getPlainObjectValues()function getPlainObjectValues(object) {return Object.keys(object).map(function (key) {return object[key];});}function getCollectionWeight(collection) {let collectionValues;if (collection instanceof Array) {collectionValues = collection;} else if (collection instanceof Map) {collectionValues = getMapValues(collection);} else {collectionValues = getPlainObjectValues(collection);}return collectionValues.reduce(function(sum, item) {return sum + getWeightByType(item);}, 0);}let myArray = [null, { }, 15];let myMap = new Map([ ['functionKey', function() {}] ]);let myObject = { 'stringKey': 'Hello world' };getCollectionWeight(myArray);// => 7 (1 + 4 + 2)getCollectionWeight(myMap);// => 4getCollectionWeight(myObject); // => 2 |
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
|
function getCollectionValues(collection) {if (collection instanceof Array) {return collection;}if (collection instanceof Map) {return getMapValues(collection);}return getPlainObjectValues(collection);} |
|
1
2
3
4
5
|
function reduceWeightSum(sum, item) {return sum + getWeightByType(item);} |
|
1
2
3
4
5
6
7
|
function getOnlyPrime(numbers) {return numbers.filter(isPrime);}getOnlyPrime([2, 3, 4, 5, 6, 8, 11]); // => [2, 3, 5, 11] |
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
function isPrime(number) {if (number === 3 || number === 2) {return true;}if (number === 1) {return false;}for (let divisor = 2; divisor <= Math.sqrt(number); divisor++) {if (number % divisor === 0) {return false;}}return true;}function getOnlyPrime(numbers) {return numbers.filter(isPrime);}getOnlyPrime([2, 3, 4, 5, 6, 8, 11]); // => [2, 3, 5, 11] |