Jquery每个都有where子句或类似的

时间:2017-07-05 11:23:30

标签: jquery each

我有这段代码

newDocumentState.documents = jQuery.grep(newDocumentState.documents,
    item => item["documentId"] !== docToRemove.name);

    $.each(newDocumentState.documents), (index, value ) => {
    this.documentState.addDocument(new DocumentMeta(value.documentId));
    });

我想知道我是否可以拥有这样或类似的东西。

$.each(newDocumentState.documents.where(x => x.documentId !== docToRemove.name),
    (index, value ) => {
    this.documentState.addDocument(new DocumentMeta(value.documentId));
    });

2 个答案:

答案 0 :(得分:1)

      var docToRemoveName ="New";
      var documents =[ 
            {"documentId": "New", "class": "class1"},
            {"documentId": "New", "class": "class2"},
            {"documentId": "Close", "class": "class3"},
            {"documentId": "send", "class": "class3"},
            {"documentId": "receive", "class": "class3"}];

        $.each(documents.filter(
            function (el) { 
               return el.documentId !== docToRemoveName ;
            }), 
            function( index, value ) {
                alert( index + ": " + value.documentId );
            });

jsfiddle链接 https://jsfiddle.net/o2gxgz9r/9600/

答案 1 :(得分:0)

根据我收到的答案,我想出了这个

   $.each(newDocumentState.documents.filter(obj => obj.documentId !== docToRemove.name),
                    (index, value) => {
                        this.documentState.addDocument(new DocumentMeta(value.documentId));
                    });

有效。 TKS。