我正在尝试编写扁平数组,但是为什么它在代码到达嵌套数据时停止检查?这是[ 1, 2, 3, [ [ [Object] ] ] ]
。
请解释为什么它停止通过嵌套数组以及为什么它没有结束。感谢
flatten = function(nestedArray, result) {
result = [];
each(nestedArray, function(item){
if(Array.isArray(item)){
result = result.concat(item);
} else {
result.push(item);
}
});
return result;
};
flatten([1, [2], [3, [[[4]]]]])
答案 0 :(得分:2)
JavaScript中没有类似each
的方法,您可以使用 Array#forEach
和recursion function。
var flatten = function(nestedArray, result) {
result = [];
// iterate over array
nestedArray.forEach(function(item) {
if (Array.isArray(item)) {
// do recursion to flatten the inner array and concatenate
result = result.concat(flatten(item));
} else {
result.push(item);
}
});
return result;
};
console.log(flatten([1, [2], [3, [[[4]]]]]));
答案 1 :(得分:0)
Dim dblMax As Double
dblMax = Application.WorksheetFunction.Max(dpws.Range("B2:P4"))
Dim chrt As Chart
Set chrt = pws.Shapes.AddChart.Chart
With chrt
.ChartArea.Left = 200
.ChartArea.Top = 0
.ChartArea.Height = 500
.ChartArea.Width = 800
.Legend.Position = xlLegendPositionBottom
.ChartType = xlColumnStacked
.HasDataTable = True
.SetSourceData Source:=dpws.UsedRange
.SeriesCollection("Forecasted % Complete").AxisGroup = 2
.SeriesCollection("Forecasted % Complete").ChartType = xlLineMarkers
.SeriesCollection("Forecasted % Complete").MarkerStyle = xlMarkerStyleSquare
.SeriesCollection("Cumulative").ChartType = xlLine
' Added the 2 lines below
.SeriesCollection("Cumulative").Format.Fill.Visible = msoFalse
.SeriesCollection("Cumulative").Format.Line.Visible = msoFalse
.Axes(xlValue).MinimumScale = 0
.Axes(xlValue).MaximumScale = dblMax + dblMax * 0.2
.Axes(xlValue, xlSecondary).MinimumScale = 0
.Axes(xlValue, xlSecondary).MaximumScale = 1
nd With