VueJS项目中过滤去重后的数组按字母排序失效如何解决
问题原因
你在执行排序操作前,已经通过array = this.json.map((input) => input.Input1)把原对象数组处理成了纯字符串数组,数组的每一项都是Input1对应的字符串值,不存在Input1属性。排序函数中读取a.Input1、b.Input1得到的都是undefined,所有比较结果都相等,所以排序逻辑没有生效。
修正方案
仅需要调整排序函数的判断逻辑,直接比较字符串本身即可,修正后代码如下:
computed: { filterInput1: function() { var array = this.json.map((input) => input.Input1); return array .filter((input, index) => array.indexOf(input) == index) .sort((a, b) => { if(a < b) return -1; if(a > b) return 1; return 0; }) .map((input) => {return {text: input, value: input}; }); } }
内容的提问来源于stack exchange,提问作者user16365685
相关产品推荐
相关产品推荐

