Repository navigation
Expand file tree
/
Copy pathupdate.html
More file actions
80 lines (69 loc) · 1.93 KB
/
Copy pathupdate.html
File metadata and controls
80 lines (69 loc) · 1.93 KB
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
75
76
77
78
79
80
<html>
<script src="http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script>
var model = {};
function recurse(current, path) {
for (key in current) {
if (current.hasOwnProperty(key)) {
if (typeof current[key] === 'object') {
recurse(current[key], path);
} else {
console.log('line', key, current[key]);
}
}
}
}
function update(path, data) {
var current = model;
var last = model;
var split = path.split('.');
for (var i = 0; i < split.length; i++) {
last = current;
current = current[split[i]];
}
var pathType = Object.prototype.toString.apply(current);
last[split[split.length - 1]] = data;
}
function test(path, data) {
model = _.cloneDeep(origModel);
update(path, data);
}
var origModel = {
player1 : {
first : 'dara',
last : 'kong',
weapons : {
left : 'pistol',
age : 10,
isnull : null
}
}
};
test('player1.weapons.left', 'change1');
console.assert(model.player1.weapons.left === 'change1');
console.log(model);
test('player1.weapons.left', { sub : 'dub1' });
console.assert(_.isEqual(model.player1.weapons.left, { sub : 'dub1' }));
console.log(model);
test('player1.weapons.mid', 'change2');
console.assert(model.player1.weapons.mid === 'change2');
console.log(model);
test('player1.weapons.mid', { sub : 'dub2' });
console.assert(_.isEqual(model.player1.weapons.mid, { sub : 'dub2' }));
console.log(model);
test('player1.weapons', { sub : 'dub2' });
console.assert(_.isEqual(model.player1.weapons, { sub : 'dub2' }));
console.log(model);
/*
string (exist) - intrinstic
string (exist) - object
string (not exist) - intrinsic
string (not exist) - object
object (exist) - intrinsic
object (exist) - object
object (not exist) - intrinsic
object (not exist) - object
*/
//recurse(model, path);
</script>
</html>