2017-08-13 12 views
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.6/lodash.min.js"></script> 

<script> 
    var topicData = []; //some data here 

    var topicList = topicData.map((d) => { 
     return d.topic; 
    }) 
    _.uniq(topicList); //not work, still the old array 

</script> 

我不知道我是否導入庫錯誤或別的東西。任何幫助表示讚賞。lodash.uniq不工作

回答

2

uniq返回一個新的數組,而不是改變現有的數組。

從文檔:

創建一個陣列的無重複的版本,使用SameValueZero爲相等比較,其中僅在每個元件的第一次出現被保持。結果值的順序由它們在數組中出現的順序決定。

所以你應該做下面的事情。

const uniqueTopicList = _.uniq(topicList) 
+0

謝謝,我應該更多地關注文檔而不僅僅是示例。 – Steveeeeee