ES6 數組去重

let str = this.state.currentSelectsName.split(',');

let newList = Array.from(new Set(str));

nreList就是去重後的數組

由此延伸下 項目中有個功能 

遍歷出框中的數組,得到["物理+化學+生物",。。。]  所以可以遍歷這個數組在每個item後追加一個”+“,再把數組轉換成字符串,再根據加號來分割 轉換成數組

this.setState(

({ subject }) => ({

currentSelects: subject.map(item => item.value),

currentSelectsName: subject.map(item => item.name + '+'),

}),

() => {

let str = this.state.currentSelectsName.join('').split('+');

let newList = Array.from(new Set(str));

this.setState({

currentClassNameArry: newList,

});

}

);

最後得出:[”物理“,"數學".......]這樣的  而且把裏面重複項刪除了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章