我对Java语言不熟悉,但是试图对以下内容进行平面映射:
我看到(5) [Array(2), Array(2), Array(2), Array(2), Array(2)]
的地方要像其他人一样分开出现。
0
:
(5) [Array(2), Array(2), Array(2), Array(2), Array(2)]
1
:
(2) [-112.2550785337791, 36.07954952145647]
2
:
(2) [-112.2549277039738, 36.08117083492122]
3
:
(2) [-112.2552505069063, 36.08260761307279]
4
:
(2) [-112.2564540158376, 36.08395660588506]
5
:
(2) [-112.2580238976449, 36.08511401044813]
6
:
(2) [-112.2595218489022, 36.08584355239394]
7
:
(2) [-112.2608216347552, 36.08612634548589]
8
:
(2) [-112.262073428656, 36.08626019085147]
9
:
(2) [-112.2633204928495, 36.08621519860091]
10
:
(2) [-112.2644963846444, 36.08627897945274]
11
:
(2) [-112.2656969554589, 36.08649599090644]
我有以下代码:
let parsedGeoCoords = _.flatten(aoiList.map((aoi) => {
return terraformer.parse(aoi.geo).coordinates
}), true)
let data = [
[["1", "3"],["2", "5"],["3", "7"]],
["4", "5"],
["5", "56"]
]
function flatten(arr) {
return arr.reduce(function (flat, toFlatten) {
return flat.concat(is2DArray(toFlatten) ? (toFlatten) : [(toFlatten)]);
}, []);
}
function is2DArray(arr) {
return Array.isArray(arr[0])
}
console.log(flatten(data))
let data = [
[["1", "3"],["2",3],["3","5"]],
["4", "5"],
["5", "56"],
"4",
"7"
]
[ [ '1', '3' ],
[ '2', 3 ],
[ '3', '5' ],
[ '4', '5' ],
[ '5', '56' ],
[ '4', '7' ] ]
答案 0 :(得分:2)
您可以固定当前输出以匹配所需的内容。只需添加:
var fullFlatten = _.flatten(_.map(data, function(elem) {
return _.flatten(elem);
}));
var chunks = _.chunk(fullFlatten, 2);
答案 1 :(得分:1)
使用lodash,您只需使用flattenDeep然后使用chunk:
let data = [ [["1", "3"],["2",3],["3","5"]], ["4", "5"], ["5", "56"], "4", "7"]
console.log(_.chunk(_.flattenDeep(data), 2)) // with no chaining
// with chaining
console.log(
_(data)
.flattenDeep()
.chunk(2)
.value()
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>