Array.filter() 在 Vue.js 中无法正常工作 [英] Array.filter() not working properly in Vue.js

查看:67
本文介绍了Array.filter() 在 Vue.js 中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决这个过滤器不起作用的问题:

Hi i'm struggling with this filter not working:

export default {
  props: {
    participants: Array,
  },
  methods: {
    filterUsersByCostCenter() {
      this.participants.filter(function (participant) {
        return (participant.cost_center == 2);
      });
    }
  }
}
</script>

这就是参与者的样子:

participants = [
  [0] => {
    'name': 'Luca',
    'surname': 'Rossi',
    'cost_center': 1
  }
  [1] => {
    'name': 'Mario',
    'surname': 'Rossi',
    'cost_center': 2
  }
]

我希望只得到第二个索引但不起作用

I expect to get only the second index as result but doesn't work

推荐答案

filter 不会改变数组,而是返回一个新数组.你可能需要

filter doesn't mutate the array, it returns a new one. You probably need to

this.participants = this.participants.filter(function (participant) {
  return (participant.cost_center == 2);
});

这篇关于Array.filter() 在 Vue.js 中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆