You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 08:24:00