在数组中搜索特定值

时间:2016-03-08 01:31:31

标签: javascript meteor

我正在尝试搜索数组以查找this._idstudentId returns fav returns正确的ID Object {_id: "fMNS3M5dW356C46Sq", emails: Array[1], profile: Object, roles: Array[2]} var fav

如何让studentId返回数组中的list.js,如果找不到ID,则返回null?

路径:Template.list.events({ 'click .favourite':function(event,template) { console.log('click'); var studentId = this._id; var fav = Meteor.users.findOne({"profile.favorites": studentId }); console.log(fav); if (candidateId === fav) { Meteor.users.update({_id: Meteor.userId()}, {$pull:{"profile.favorites":studentId}}); } else { Meteor.users.update({_id: Meteor.userId()}, {$push:{"profile.favorites":studentId}}); }; } });

public class RewriteArray {
    public static void main(String[] args) {
        String[] arr = { "1", "2", "3"};

        System.out.print("Before rewrite: ");
        for (String str : arr)
            System.out.print(str + " ");
        System.out.println();

        changeArr(arr);

        System.out.print("After rewrite:  ");
        for (String str : arr)
            System.out.print(str + " ");
        System.out.println();
    }

    private static void changeArr(String[] arr) {
        arr[0] = "3";
        arr[1] = "2";
        arr[2] = "1";
    }
}

1 个答案:

答案 0 :(得分:1)

根据您的评论,我认为您想要:

Template.list.events({
  'click .favourite':function(event,template) {
    console.log('click');
    var studentId = this._id;
    var fav = null;

    if( Meteor.users.findOne({"profile.favorites":  studentId }) ) {
      fav = studentId;
    }
    if (candidateId === fav) {
      Meteor.users.update({_id: Meteor.userId()}, {$pull:{"profile.favorites":studentId}});
    } else {
      Meteor.users.update({_id: Meteor.userId()}, {$push:{"profile.favorites":studentId}});
    };
  }
});

如果Meteor.users.findOne返回某个内容,则会将fav设置为studentId。