Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Vuejs2-如何使用计算属性比较两个数组中的元素?_Javascript_Arrays_Vuejs2_Computed Properties - Fatal编程技术网

Javascript Vuejs2-如何使用计算属性比较两个数组中的元素?

Javascript Vuejs2-如何使用计算属性比较两个数组中的元素?,javascript,arrays,vuejs2,computed-properties,Javascript,Arrays,Vuejs2,Computed Properties,我正在做一个比较两个数组的计算函数。。。一个是数据中的关键字,另一个是通过计算的关键字列表返回的。当计算比较时,他不接受Axios在第一个数组中获得的计数值。。。 我不知道该怎么办 我的代码: { beforeCreate() { this.$axios({ method: 'get', url: '/api/rest/alerts/' }).then(response =&g

我正在做一个比较两个数组的计算函数。。。一个是数据中的
关键字
,另一个是通过计算的
关键字列表
返回的。当计算比较时,他不接受Axios在第一个数组中获得的计数值。。。 我不知道该怎么办

我的代码:

{
    beforeCreate() {
            this.$axios({
                method: 'get',
                url: '/api/rest/alerts/'
            }).then(response => {
                if ((response.data.keywords.length == 0) && (response.data.countries.length == 0)) {
                    this.alertEmpty = 'Aucune alerte de créée'
                } else {
                    this.alert = response.data
                    this.keywords = response.data.keywords
                    this.countriesKeywords = response.data.countries
                    this.allSectorsSelected = response.data.all_sectors
                }
            })
        },

        data() {
            return {
                categories: "",
                alert: '',
                alertEmpty: '',
                countriesKeywords: [],
                countrySelected: '',
                keywords: [],
                update: false,
                sectorSelected: "",
                alerts: [],
                keywordSelected: "",
                errorKeyword: '',
                successModification: '',
                allSectorsSelected: null,

            };
        },

        computed: {
            keywords_list: function() {
                for (var i = 0; i < this.sectors.length; i++) {
                    if (this.sectors[i].id == this.sectorSelected) {
                        return this.sectors[i].keywords;
                    }
                }
            },
            allKeywordsInSector: function() {
                if (this.keywords_list.every(item => this.keywords.indexOf(item) >= 0)) {
                    return true;
                }
                return false;
            },
        }
}
{
beforeCreate(){
这是axios({
方法:“get”,
url:“/api/rest/alerts/”
})。然后(响应=>{
if((response.data.keywords.length==0)和&(response.data.countries.length==0)){
this.alertEmpty='Aucune alerte de crée'
}否则{
this.alert=response.data
this.keywords=response.data.keywords
this.countriesKeywords=response.data.countries
this.allsectorselected=response.data.all_扇区
}
})
},
数据(){
返回{
类别:“,
警报:“”,
警报为空:“”,
countriesKeywords:[],
选定的国家:“”,
关键词:[],
更新:错误,
部门:,
警报:[],
关键字已选定:“”,
errorKeyword:“”,
成功修改:“”,
allSectorsSelected:空,
};
},
计算:{
关键词列表:函数(){
对于(var i=0;ithis.keywords.indexOf(item)>=0)){
返回true;
}
返回false;
},
}
}

感谢您的帮助

非常少的信息,而且不清楚api结果如何影响您的计算值 我唯一能说的是,Vue中的属性没有反应。因此,如果
sections
不是静态的,则计算属性
sectorSelected
只有在
sectorSelected
更改时才会更改


信息很少,也不清楚api结果如何影响计算值 我唯一能说的是,Vue中的属性没有反应。因此,如果
sections
不是静态的,则计算属性
sectorSelected
只有在
sectorSelected
更改时才会更改


从api中获取数据后,您的
关键字
变量最终如何查找?她正在查找关键字:[31]内部有31个对象,但如果我再次将所有关键字添加到计算中,则在计数中,当条件
this.sections[i].id==this.sectorSelected
失败时,您的计算属性
关键字列表
可能是
未定义的。
这个扇区是从哪里来的?我在数据对象或API调用中没有看到它。如果
keywords\u list
未定义
内部有31个对象,但如果我再次将所有关键字添加到计算中,则在计数中,当条件
this.sections[i].id==this.sectorSelected
失败时,您的计算属性
关键字列表
可能是
未定义的。
这个扇区是从哪里来的?我在数据对象或API调用中没有看到它。如果
关键字列表
未定义
,则
所有关键字扇区
的计算函数可能会出错。谢谢您的回答!是的,我试过很多东西,但问题不是来自部门。。。我已经在
扇区上创建了一个监视,选中了
并显示console.log,但条件不起作用,如果我删除所有旧关键字并添加新闻关键字,它就正常了。。。我不明白为什么。。。看起来数组中的旧关键字不算在内…嘿,谢谢你的回答!是的,我试过很多东西,但问题不是来自部门。。。我已经在
扇区上创建了一个监视,选中了
并显示console.log,但条件不起作用,如果我删除所有旧关键字并添加新闻关键字,它就正常了。。。我不明白为什么。。。看起来数组中的旧关键字没有计数。。。