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

Vue中调用this.boss_type.filter报错:filter不是函数,求协助

Fix: TypeError: this.boss_type.filter is not a function

Hey there! The issue here is super straightforward: your boss_type is an object, but filter() is a method that only works on arrays. Objects don’t have a built-in filter method, which is why you’re seeing that error pop up.

How to fix it

You need to convert your object into an array first, then use filter() on that array. Here are a few common ways to do this depending on what you need:

  1. If you only care about the values in the object
    Use Object.values() to get an array of all the values in boss_type:

    test: function() {
      Object.values(this.boss_type).filter((value) => {
        console.log(value);
        // Add your actual filtering condition here, e.g.:
        // return value !== 'Some value';
      });
    }
    
  2. If you need both keys and values
    Use Object.entries() to get an array of [key, value] pairs:

    test: function() {
      Object.entries(this.boss_type).filter(([key, value]) => {
        console.log(`Key: ${key}, Value: ${value}`);
        // Example filter condition: keep only entries for 'prisoner' or 'guardian'
        return ['prisoner', 'guardian'].includes(key);
      });
    }
    
  3. If you only need the keys
    Use Object.keys() to get an array of the object’s keys:

    test: function() {
      Object.keys(this.boss_type).filter((key) => {
        console.log(key);
        // Example filter condition: keep keys that contain 'secret'
        return key.includes('secret');
      });
    }
    

Quick recap

Just remember: filter() is exclusively for arrays. When working with objects, use Object.values(), Object.entries(), or Object.keys() to convert the object into an array structure that you can then filter, map, or iterate over as needed.

内容的提问来源于stack exchange,提问作者Eugene Starosvetskiy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 07:18:57