-
Notifications
You must be signed in to change notification settings - Fork 0
/
example__setNestedValue.js
70 lines (50 loc) · 963 Bytes
/
example__setNestedValue.js
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
const {setNestedValue} = require('../lib/index');
let obj = {
name: 'Vasi'
}
let key = 'name';
setNestedValue(obj, key, 'karan');
console.log(obj.name);
/*
output,
'Karan'
*/
obj.phoneNo = {
primary: 1234567890,
secondary: 0987654321
}
key = 'phoneNo.primary';
setNestedValue(obj, key, 12121212);
console.log(obj.phoneNo.primary);
/*
output,
12121212
*/
obj.users = {
user1: {
names: {
firstName: 'Vasi',
secondName: 'Karan'
},
address: {
streets: {
old: 'bharathiyar st',
new: 'Main Road',
}
}
}
}
key = 'users.user1.names.secondName';
setNestedValue(obj, key, 'Raina');
console.log(obj.users.user1.names.secondName);
/*
output,
Raina
*/
key = 'users.user1.address.streets.new';
setNestedValue(obj, key, 'New street');
console.log(obj.users.user1.address.streets.new);
/*
output,
New street
*/