Vue中调用this.boss_type.filter报错:filter不是函数,求协助
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:
If you only care about the values in the object
UseObject.values()to get an array of all the values inboss_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'; }); }If you need both keys and values
UseObject.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); }); }If you only need the keys
UseObject.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

