Javascript 如何从数组中排除某些元素?(vue.js)
所以,基本上,我使用一个HTML选择填充注册用户的数组Javascript 如何从数组中排除某些元素?(vue.js),javascript,vue.js,Javascript,Vue.js,所以,基本上,我使用一个HTML选择填充注册用户的数组 <label > <b> <i style="opacity:0.8">Users:</i> </b> </label> <select class="form-control w-50 mb-5" style="display:inline;&quo
<label >
<b>
<i style="opacity:0.8">Users:</i>
</b>
</label>
<select
class="form-control w-50 mb-5"
style="display:inline;"
v-model="filter"
@change="userfilter"
>
<option id="userbox" v-for="user in userlist" :key="user.userlist">{{user}}</option>
<option>All</option>
</select>
我还创建了一个supper tarraysupportarchive:[]
,当此条件结果为真时,该函数将填充原始数组:
methods: {
userfilter: function() {
this.selecteduser = this.filter;
if (this.filter == "All") {
this.supportarchive=[];
this.archive = JSON.parse(localStorage.getItem("completedtasks"));
} else {
this.supportarchive = this.archive;
this.archive = [];
for (let i = 0; i < this.supportarchive.length; i++) {
if (this.supportarchive.taskuserID == this.selecteduser) {
this.archive.push(this.supportarchive[i]);
}
}
}
}
}
方法:{
userfilter:function(){
this.selecteduser=this.filter;
如果(this.filter==“全部”){
this.supportarchive=[];
this.archive=JSON.parse(localStorage.getItem(“completedtasks”);
}否则{
this.supportarchive=this.archive;
this.archive=[];
for(设i=0;i
好的,如果
过滤器
是全部,那么我从本地存储
填充数组,但是如果过滤器
有另一个值,那么我在支持存档
中备份我的原始数组存档
,然后我清空原始文件,如果满足条件,我将supportarchive
中的元素推入原始文件存档
,但它不起作用,我不知道为什么是这样。supportarchive
是对象而不是数组?那么它就没有length属性,也不会在循环中工作
一种解决方案是获取对象关键点并基于这些关键点进行循环
var keys = Object.keys(this.supportarchive)
var tempArr = []
for (let i = 0; i < keys.length; i++) {
let item = this.supportarchive[keys[i]];
if (item.taskuserID == this.selecteduser) {
// You should put this into a new temporary array.
// Dont change the array you are looping
// this.archive.push(this.supportarchive[i]);
tempArr = item
}
}
// Parse back into json
this.archive = JSON.parse(tempArr);
var keys=Object.keys(this.supportarchive)
var tempArr=[]
for(设i=0;i
在代码中执行类似于的操作。supportarchive[i]。taskuserID
methods: {
userfilter: function() {
this.selecteduser = this.filter;
if (this.filter == "All") {
this.supportarchive=[];
this.archive = JSON.parse(localStorage.getItem("completedtasks"));
} else {
this.supportarchive = this.archive;
this.archive = [];
for (let i = 0; i < this.supportarchive.length; i++) {
if (this.supportarchive[i].taskuserID == this.selecteduser) {
this.archive.push(this.supportarchive[i]);
}
}
}
}
}
methods: {
userfilter: function() {
this.selecteduser = this.filter;
if (this.filter == "All") {
this.supportarchive=[];
this.archive = JSON.parse(localStorage.getItem("completedtasks"));
} else {
this.supportarchive = this.archive;
this.archive = [];
for (let i = 0; i < this.supportarchive.length; i++) {
if (this.supportarchive[i].taskuserID == this.selecteduser) {
this.archive.push(this.supportarchive[i]);
}
}
}
}
}
this.archive = this.supportarchive.filter(u => u.taskuserID === this.selecteduser);