Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
242 views
in Technique[技术] by (71.8m points)

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

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>

This is what participants looks like:

(参与者的样子如下:)

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

(我希望仅获得第二个索引,但不起作用)

  ask by Luca Rossi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

filter doesn't mutate the array, it returns a new one.

(filter不改变数组,它返回一个新的数组。)

You probably need to

(您可能需要)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...