刪除空:的JavaScript我有機能缺失被合併兩個數組這樣的對象
var sorting = data.genre.map((i) => data_info.find((o) => o.id === i));
在這個函數的問題是,如果它不能在data_info
找到genre
其添加null
對象ARRA因此我的功能失敗了。
所以問題是:我怎樣才能在上面的單行刪除所有值爲空?
我從上面THR線輸出:
[{"id":1,"category":"Action"},{"id":2,"category":"Adventure"},null]
,並需要得到這樣的:
[{"id":1,"category":"Action"},{"id":2,"category":"Adventure"}]
我試着在上面添加&& data_info(o) !== null
到行,但我的目標得到null
無論如何。
在'map'的輸出中添加一個'filter'。 'data.genre.map((i)=> data_info.find((o)=> o.id === i))。filter((i)=> return i!= null);' – gurvinder372
map function對數組中的每個項應用該操作,並返回一個數組,其元素數與輸入數組中的元素數完全相同。 – ajaysinghdav10d
[如何通過值從數組中刪除項目]可能的重複(https://stackoverflow.com/questions/3954438/how-to-remove-item-from-array-by-value) –