forked from sethvincent/object-array-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
32 lines (27 loc) · 679 Bytes
/
Copy pathindex.js
File metadata and controls
32 lines (27 loc) · 679 Bytes
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
exports.toArray = function (obj) {
obj = obj || {}
var result = []
Object.keys(obj).forEach(function (key, i) {
result.push(Object.assign({
$key: key
}, obj[key]));
// result.push({ key: key, value: obj[key] })
})
return result
}
exports.toObject = function (arr) {
arr = arr || []
var result = {}
arr.forEach(function (obj, i) {
// if (typeof obj === 'object' && obj.key && obj.value) {
if (typeof obj === 'object' && obj.$key) {
const key = obj.$key;
delete obj.$key;
result[key] = obj;
// result[obj.$key] = obj.value
} else {
result[String(arr.indexOf(obj))] = obj
}
})
return result
}