【问题标题】:Array.filter() not working properly in Vue.js [duplicate]Array.filter() 在 Vue.js 中无法正常工作 [重复]
【发布时间】:2020-03-24 00:28:29
【问题描述】:

您好,我正在为这个过滤器不工作而苦苦挣扎:

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
  }
]

我希望只获得第二个索引作为结果但不起作用

【问题讨论】:

  • 您的 filterUsersByCostCenter 不会返回(或分配)任何内容,添加“不起作用”以外的描述实际上会暗示这一点。

标签: javascript laravel vue.js


【解决方案1】:

filter 不会改变数组,它会返回一个新数组。你可能需要

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-06
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 2015-09-06
    相关资源
    最近更新 更多