我有這個代碼:
const myFunc = function (t) {
return myArray.flatMap(clip =>
(t < clip.start || t < clip.end) ? // Valid objects are returned in this *if* condition
[
{ time: clip.start },
{ time: clip.end }
] : // how to return nothing in this *else* condition. Absolutely nothing?
[
{ },
{ }
]
)
}
上面的代碼使用了一個三元運算子condition ? exprIfTrue : exprIfFalse。
目前,{ }在exprIfFalse.
在 的情況下我怎樣才能什么都不回傳exprIfFalse?我的意思是,我絕對什么都不想要。我的意思是沒有陣列元素。
uj5u.com熱心網友回復:
為什么你不能只回傳一個空陣列,不管如何Array.flat從最終代碼中洗掉這些空陣列。在您的情況下,陣列不為空,因為[]它是一個包含[{}, {}]兩個空物件的陣列,因為它將{}, {}在最終輸出中產生兩個空物件Array.flat
你必須從flatMap. 如果不回傳任何內容,則相應的節點將添加為undefined. 這不會被洗掉Array.flat。最好的選擇是回傳一個空陣列,如下所示。
偽代碼
const myArray = [1, 2, 3, 4, 5];
const myFunc = function (t) {
return myArray.flatMap(clip =>
(clip % 2 === 0) ? // Valid objects are returned in this *if* condition
[
{ value: clip },
{ value: clip }
] : // how to return nothing in this *else* condition. Absolutely nothing?
[]
)
}
console.log(myFunc());
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/359486.html
標籤:javascript 平面图
上一篇:Github操作-CI因構建工件而卡住(將typescript轉換為javascript)
下一篇:從JSPromise計算值
