You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The mapping object is a simple object, where the `key` is the **source** and the `value` is the **destination**.
18
+
A mapping object `key` is the **source**`key`and the `value` is the `key` on the **destination** object the `value` is mapped to.
19
19
20
20
###Source###
21
21
22
-
The source can be specified as a simple string like:
22
+
The source `key`can be specified as a simple string:
23
23
24
24
```javascript
25
25
{
26
-
"foo":"bar"
26
+
"foo":"bar"//map src.foo to dest.bar
27
27
}
28
28
```
29
29
30
-
You may specify properties deep within the source object to be copied to
31
-
properties deep within the destination object by using dot notation in the
32
-
mapping like:
30
+
You may specify properties deep within the source `Object` to be copied to
31
+
properties deep within the destination `Object` by using dot notation in the
32
+
mapping `key`:
33
33
34
34
```javascript
35
35
{
36
-
"foo":"bar.baz"
37
-
, "bar.foo":"baz"
36
+
"foo":"bar.baz", //map src.foo to dest.bar.baz
37
+
"bar.foo":"baz"//map src.bar.foo to dest.baz
38
38
}
39
39
```
40
40
41
-
You may also specify array lookups within the source object to be copied to properties deep within the destination object by using `[]` notation in the mapping link:
41
+
You may also specify `Array` lookups within the source `Object` to be copied to properties deep within the destination object by using `[]` notation in the mapping:
42
42
43
43
```javascript
44
44
{
45
-
"[].foo":"bar[]"
46
-
, "foo[].bar":"[]"
47
-
, "foo[0].bar":"baz"
45
+
"[].foo":"bar[]",
46
+
"foo[].bar":"[]",
47
+
"foo[0].bar":"baz"
48
48
}
49
49
```
50
50
@@ -57,67 +57,80 @@ You may specify the destination as:
57
57
58
58
####String####
59
59
60
-
When using string as destination you shall use as described above.
60
+
When using a `String`as the destination, use the method described above.
61
61
62
-
In order to utilize a source field more than once, utilize the key-transform syntax in the mapping link:
62
+
To utilize a source field more than once, utilize the key-transform syntax in the mapping link:
63
63
64
64
```javascript
65
-
{
65
+
var objectMapper =require('object-mapper');
66
+
67
+
var map = {
66
68
"foo": [
67
69
{
68
70
key:"foo",
69
-
transform:function (value) {
71
+
transform:function (value) {
70
72
return val +"_foo";
71
73
}
72
74
},
73
75
{
74
76
key:"baz",
75
77
transform:function (value) {
76
-
return val +"_baz"
78
+
return val +"_baz";
77
79
}
78
80
}
79
81
],
80
82
"bar":"bar"
81
-
}
83
+
};
84
+
85
+
var src = {
86
+
foo:'blah',
87
+
bar:'something'
88
+
};
89
+
90
+
var dest =objectMapper(src, map);
91
+
92
+
// dest.foo: 'blah_foo'
93
+
// dest.baz: 'blah_baz'
94
+
// dest.bar: 'something'
82
95
```
83
96
84
97
####Object####
85
98
86
-
When using object as destination you need can use the following object:
By default any source object null value is not mapped. If you want to allow this you may do so explicitly by including the post fix operator '?' to any destination key.
162
+
By default `null` values on the source `Object`is not mapped. You can override this by including the post fix operator '?' to any destination `key`.
150
163
151
164
```javascript
152
165
var original = {
@@ -180,7 +193,7 @@ This function is also exported directly from `require('object-mapper')` (ie: `va
180
193
181
194
### .getKeyValue(sourceObject, key);
182
195
183
-
Get the key value within **sourceObject**, going deep within the object if necessary.
196
+
Get the `key` value within **sourceObject**, going deep within the object if necessary.
184
197
This method is used internally but is exposed because it may be of use elsewhere
185
198
with other projects.
186
199
@@ -189,7 +202,7 @@ with other projects.
189
202
190
203
### .setKeyValue(destinationObject, key, value);
191
204
192
-
Set the key value within **destinationObject**, going deep within the object if necessary.This
205
+
Set the `key` value within **destinationObject**, going deep within the object if necessary.This
193
206
method is used internally but is exposed because it may be of use elsewhere with
0 commit comments