-
-
Save chengjianhua/e5760974aa43d0463d935e350a91ca78 to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/jabakicowi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script src="https://cdn.jsdelivr.net/lodash/4/lodash.min.js"></script> | |
<script id="jsbin-javascript"> | |
'use strict'; | |
var data = { | |
a: { | |
b: [{ | |
c: [{ | |
d: 'AB11' | |
}, { | |
d: 'AB12' | |
}, { | |
d: 'AB13' | |
}] | |
}, { | |
c: [{ | |
d: 'AB21' | |
}, { | |
d: 'AB22' | |
}, { | |
d: 'AB23' | |
}] | |
}, { | |
c: [{ | |
d: 'AB31' | |
}, { | |
d: 'AB32' | |
}, { | |
d: 'AB33' | |
}] | |
}] | |
} | |
}; | |
var name = 'a.b[].c[].d'; | |
var parts = name.split('[]'); | |
// console.log(parts); | |
// console.log(_.get(data, parts[0]).map((value, index) => `${parts[0]}[${index}]`)); | |
var result = parts.slice(1).reduce(function (accu, part, partIndex) { | |
// console.log(part); | |
accu = accu.map(function (path) { | |
// console.log(`当前的 path = ${path}`); | |
var newPath = path + part; | |
var partData = _.get(data, newPath); | |
// console.log(`${newPath}, ${JSON.stringify(partData)}`) | |
if (_.isArray(partData)) { | |
return partData.map(function (value, index) { | |
return newPath + '[' + index + ']'; | |
}); | |
} | |
return newPath; | |
}); | |
return _.flatten(accu); | |
}, _.get(data, parts[0]).map(function (value, index) { | |
return parts[0] + '[' + index + ']'; | |
})); | |
console.log('\n' + result.join('\n') + '\n'); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const data = { | |
a: { | |
b: [ | |
{ | |
c: [ | |
{ | |
d: 'AB11' | |
}, | |
{ | |
d: 'AB12' | |
}, | |
{ | |
d: 'AB13' | |
}, | |
] | |
}, | |
{ | |
c: [ | |
{ | |
d: 'AB21' | |
}, | |
{ | |
d: 'AB22' | |
}, | |
{ | |
d: 'AB23' | |
}, | |
] | |
}, | |
{ | |
c: [ | |
{ | |
d: 'AB31' | |
}, | |
{ | |
d: 'AB32' | |
}, | |
{ | |
d: 'AB33' | |
}, | |
] | |
} | |
] | |
} | |
}; | |
const name = 'a.b[].c[].d'; | |
const parts = name.split('[]'); | |
// console.log(parts); | |
// console.log(_.get(data, parts[0]).map((value, index) => `${parts[0]}[${index}]`)); | |
const result = parts.slice(1).reduce((accu, part, partIndex) => { | |
// console.log(part); | |
accu = accu.map((path) => { | |
// console.log(`当前的 path = ${path}`); | |
const newPath = path + part; | |
const partData = _.get(data, newPath); | |
// console.log(`${newPath}, ${JSON.stringify(partData)}`) | |
if (_.isArray(partData)) { | |
return partData.map((value, index) => `${newPath}[${index}]`); | |
} | |
return newPath; | |
}); | |
return _.flatten(accu); | |
}, _.get(data, parts[0]).map((value, index) => `${parts[0]}[${index}]`)); | |
console.log('\n' + result.join('\n') + '\n') | |
</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var data = { | |
a: { | |
b: [{ | |
c: [{ | |
d: 'AB11' | |
}, { | |
d: 'AB12' | |
}, { | |
d: 'AB13' | |
}] | |
}, { | |
c: [{ | |
d: 'AB21' | |
}, { | |
d: 'AB22' | |
}, { | |
d: 'AB23' | |
}] | |
}, { | |
c: [{ | |
d: 'AB31' | |
}, { | |
d: 'AB32' | |
}, { | |
d: 'AB33' | |
}] | |
}] | |
} | |
}; | |
var name = 'a.b[].c[].d'; | |
var parts = name.split('[]'); | |
// console.log(parts); | |
// console.log(_.get(data, parts[0]).map((value, index) => `${parts[0]}[${index}]`)); | |
var result = parts.slice(1).reduce(function (accu, part, partIndex) { | |
// console.log(part); | |
accu = accu.map(function (path) { | |
// console.log(`当前的 path = ${path}`); | |
var newPath = path + part; | |
var partData = _.get(data, newPath); | |
// console.log(`${newPath}, ${JSON.stringify(partData)}`) | |
if (_.isArray(partData)) { | |
return partData.map(function (value, index) { | |
return newPath + '[' + index + ']'; | |
}); | |
} | |
return newPath; | |
}); | |
return _.flatten(accu); | |
}, _.get(data, parts[0]).map(function (value, index) { | |
return parts[0] + '[' + index + ']'; | |
})); | |
console.log('\n' + result.join('\n') + '\n'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment