I want to filter array of objects by index.
<ul>
<li v-for="(list,index) in lists" v-bind:key="index"
@dblclick="deleteNote(index)">
{{list.note}
</li>
</ul>
Lists gets filled with objects of imp
var lists = [];
var imp = {
note: "bla",
hinweis: "blub",
showNotiz: false
};
deleteNote(i) {
let arr = this.lists.filter(item =>
item.note !== this.lists[i]
);
this.lists = arr;
}
I want to filter array of objects by index.
<ul>
<li v-for="(list,index) in lists" v-bind:key="index"
@dblclick="deleteNote(index)">
{{list.note}
</li>
</ul>
Lists gets filled with objects of imp
var lists = [];
var imp = {
note: "bla",
hinweis: "blub",
showNotiz: false
};
deleteNote(i) {
let arr = this.lists.filter(item =>
item.note !== this.lists[i]
);
this.lists = arr;
}
Share
Improve this question
edited Mar 13, 2019 at 11:12
adiga
35.2k9 gold badges65 silver badges87 bronze badges
asked Mar 13, 2019 at 11:09
quinzo quinzo
6892 gold badges9 silver badges27 bronze badges
1
- First, note is a string and lists[i] is an object. Or can note be an object ? – DEVCNN Commented Mar 13, 2019 at 12:40
3 Answers
Reset to default 18I think this will works
deleteNote(i) {
this.lists = this.lists.filter((_, index) => index !== i);
}
You need to use second argument to the filter function.
let arr = this.lists.filter( (item, index) =>
item.note !== this.lists[index]
);
this.lists = arr;
Here is MDN docs for Filter
Hope this helps!
By the looks of it, you want to remove an item by index?
deleteNote(i) {
this.lists.splice(i, 1);
}
The above snippet should modify the existing array and remove one element at the desired index.
MDN: Array.prototype.splice()