-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
74 lines (70 loc) · 2.07 KB
/
test.html
1
2
3
4
5
6
7
8
9
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
<!--
* @Descripttion:
* @Author: tom-z(spirit108@foxmail.com)
* @Date: 2020-03-27 10:25:43
* @LastEditors: tom-z(spirit108@foxmail.com)
* @LastEditTime: 2020-12-26 11:10:59
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
function doCombination(arr) {
var count = arr.length - 1; //数组长度(从0开始)
var tmp = [];
var totalArr = [];// 总数组
return doCombinationCallback(arr, 0);//从第一个开始
//js 没有静态数据,为了避免和外部数据混淆,需要使用闭包的形式
function doCombinationCallback(arr, curr_index) {
for(val of arr[curr_index]) {
tmp[curr_index] = val;//以curr_index为索引,加入数组
//当前循环下标小于数组总长度,则需要继续调用方法
if(curr_index < count) {
doCombinationCallback(arr, curr_index + 1);//继续调用
}else{
totalArr.push(tmp);//(直接给push进去,push进去的不是值,而是值的地址)
}
//js 对象都是 地址引用(引用关系),每次都需要重新初始化,否则 totalArr的数据都会是最后一次的 tmp 数据;
oldTmp = tmp;
tmp = [];
for(index of oldTmp) {
if (index) {
tmp.push(index);
}
}
}
return totalArr;
}
}
//测试数组
var arr = [
[1,2,3,4,5,6],
[''],
['成功', '失败']
];
//调用方法
console.log(doCombination(arr))
let obj = [1, {a: 1, b: 2}];
let proxyObj = new Proxy(obj, {
set(tartget, key, val) {
console.log(555)
tartget[key] = val;
},
get(target, key) {
return target[key];
}
})
console.log(proxyObj);
proxyObj[0] = 4;
proxyObj[1] = "444"
console.log(proxyObj);
// document.write(doCombination(arr));
</script>
</body>
</html>