最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Filter array of objects by index - Stack Overflow

programmeradmin8浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 18

I 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()

发布评论

评论列表(0)

  1. 暂无评论