Javascript Nuxt方法调用另一个方法:PROMITE不是函数
在nuxt.js(vue.js)上,我必须生成一个过滤JSON的函数。 此JSON包含若干人:Javascript Nuxt方法调用另一个方法:PROMITE不是函数,javascript,filter,nuxt.js,Javascript,Filter,Nuxt.js,在nuxt.js(vue.js)上,我必须生成一个过滤JSON的函数。 此JSON包含若干人: couple: [ { id: 1, pers1: { id: 1, firstname: 'Vincent', }, pers2: { id: 1, firstname: 'Leslie', }, }, { id: 2, pers1: { id:
couple: [
{
id: 1,
pers1: {
id: 1,
firstname: 'Vincent',
},
pers2: {
id: 1,
firstname: 'Leslie',
},
},
{
id: 2,
pers1: {
id: 3,
firstname: 'Leslie',
},
pers2: {
id: 4,
firstname: 'Henri',
},
},
]
有一个表单允许用户搜索人名。
如果他搜索“Leslie”,结果必须是:
- 文森特和莱斯利
- 莱斯利和亨利
- 莱斯利和亨利
methods:{
async searchCouple() {
this.filterCouples.filter(this.makeFilter(this.firstname));
},
async makeFilter(search){
return function(element){
return element.couple.pers1.firstname.toUpperCase() === search || element.couple.pers2.firstname.toUpperCase() === search;
}
}
但我有一个错误:
TypeError: #<Promise> is not a function
没关系(但只对一个人)。但如果我写:
async searchCouple() {
this.filterCouples = this.couple.filter(a => a.pers1.firstname.toUpperCase() === this.firstname.toUpperCase());
}
async searchCouple() {
this.filterCouples = this.couple.filter(a => a.pers1.firstname.toUpperCase() === this.firstname.toUpperCase() || a.pers2.firstname.toUpperCase() === this.firstname.toUpperCase());
}
吊杆:(
有什么想法吗?
非常感谢
编辑:
我使用以下命令更改代码:
methods: {
async searchCouple() {
this.filterCouple.filter(this.makeFilter(this.firstname));
},
makeFilter(search){
return function(element){
if(element.pers1.firstname.toUpperCase() == search.toUpperCase()
|| element.pers2.firstname.toUpperCase() == search.toUpperCase()){
return true;
}else{
return false;
}
}
}
My函数返回正确值(console.log):
或:
但是:filterCouple返回所有我的json,尽管函数的结果是:(a
async
函数将始终返回一个承诺。您的两个函数都不需要标记为async
,因此删除该函数将解决您的问题error@NickParsons谢谢,就是这样。现在我写:this.filterCouples.filter(this.makeFilter(this.firstname));但是:我的函数makeFilter返回true或false(正确),但是this.filterCouples仍然返回我的所有JSON。filter方法返回一个新数组,它不会更改调用filter方法的数组。您可以将结果存储在一个新变量中(建议)或者将返回值重新分配回filterCouples@NickParsons我爱你,非常感谢@NickParsons:也许对你来说是个额外的问题:我必须对姓氏进行同样的搜索。我只想保留一个函数。所以我使用了第二个参数并编写了:this.filterCouples.filter(this.makeFilter(this.firstname),'firstname');在我的函数中:makeFilter(search,node){…},如何使用node参数?例如:element.pers1.{{node}==?
Vincent IN Vincent / Leslie = true
Vincent IN Leslie / Henri = false
Leslie IN Vincent / Leslie = true
Leslie IN Leslie / Henri = true