Skip to content

Commit 0117ca8

Browse files
committed
nested arrays mapping correctly
1 parent 256ecdd commit 0117ca8

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

index.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,45 @@
11
var mapObj = require('map-obj');
22
var camelCase = require('camelcase');
3-
var isArray = require('isarray');
3+
var _ = require('lodash');
44

55
function camelCaseRecursive(obj) {
66
return mapObj(obj, function(key, val) {
77
var newArray = [];
88

9-
if (isArray(val)) {
9+
if (_.isArray(val)) {
1010

1111
val.forEach(function(value) {
12-
newArray.push(camelCaseRecursive(value));
12+
if(_.isObject(value) && !_.isArray(value)){
13+
newArray.push(camelCaseRecursive(value));
14+
}else{
15+
newArray.push(value);
16+
}
1317
});
1418

1519
return [camelCase(key), newArray];
16-
17-
} else if (typeof(val) === 'object') {
18-
20+
21+
} else if (_.isObject(val)) {
22+
1923
return [camelCase(key), camelCaseRecursive(val)];
20-
24+
2125
} else {
22-
26+
2327
return [camelCase(key), val];
24-
28+
2529
}
2630
});
2731
}
2832

29-
module.exports = camelCaseRecursive;
33+
module.exports = camelCaseRecursive;
34+
35+
36+
var anotherCamelWithTheHump = camelCaseRecursive({
37+
'test-1': 123,
38+
'test-Two': [{
39+
'test-three': {
40+
'test-FOUR': [{'test-five':[{'test-six':{'test-seven':[1,4,[1,2,'3','four', 'five-one']]}}]}]
41+
}
42+
}]
43+
});
44+
45+
console.log(JSON.stringify(anotherCamelWithTheHump));

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"dependencies": {
2020
"camelcase": "^1.0.2",
2121
"isarray": "0.0.1",
22+
"lodash": "^3.1.0",
2223
"map-obj": "^1.0.0"
2324
}
2425
}

0 commit comments

Comments
 (0)