-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml2json.js
40 lines (38 loc) · 1.47 KB
/
xml2json.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
const lodash = require('lodash');
function flatten(obj, path = [], result = {}, resArray = [], resNames = []) {
// pick the type, name and attributes
let iWanted = lodash.pick(obj, ["type", "name", "attributes"]);
if (Object.keys(iWanted).length > 0) {
// add the xmlpath to the object;
Object.assign(iWanted, { xmlpath: resNames.join('>')});
resArray.push(iWanted);
}
if (obj && typeof obj === 'object') {
Object.keys(obj).forEach(key => {
const value = obj[key];
const newPath = path.concat(key);
//
let newResNames=[];
if(Number.isInteger(Number(key))) {
let eItem = `${value.name}[${key}]`;// button[0]
if(Number(key)>0){
let beDeleted = `${value.name}[${key-1}]`
let index = resNames.lastIndexOf(beDeleted);
let resNamesCopy = resNames.slice(0, index);
newResNames = resNamesCopy.concat(eItem);
}else{
newResNames = resNames.concat(`${value.name}[${key}]`);
}
resNames=newResNames;
}
//
if (value && typeof value === 'object') {
flatten(value, newPath, result, resArray, resNames);
} else {
result[newPath.join('.')] = value;
}
});
}
return resArray
}
module.exports = flatten;