使用数组来过滤数组的Javascript [英] Using an array to filter an array Javascript

查看:114
本文介绍了使用数组来过滤数组的Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想看看每个值在一个阵列中,看看其中是否包含在其他任何数组值。
例如:

I am trying to look at every value in one array and see if any of them are contained in any of the other array values. Ex:

arrOne = ['a', 'b' ,'c' ];
arrTwo = ['option a', 'option c', 'option b', 'option d'];

所以,我希望它循环看到arrTwo每个值有从arrOne的一个值,并删除不要让我得到的:

So I want it to cycle through to see every value in arrTwo that has one of the values from arrOne and remove the ones that dont so I get:

arrFinal = ['option a', 'option c', 'option b'];

下面是code我一起工作:

Here is the code I am working with:

JavaScript的

JavaScript

function PreSaveAction() {
var Fields = ['input','select','textarea']
var ReqEmpty = 0;
var RowNum = ( $('table.input-table > tbody > tr').length);
var type = [];
var typeFinal = [];
var FieldType;
var FieldTypeNum;

for(var i=0; i<=(RowNum - 1); i++){
    type.push( $("table.input-table > tbody > tr").eq(i).html());
};
alert(type.length); 



//Where a need the filter code
//using Req as arrOne and type as arrTwo




for(var i = 0; i <= (type.length - 1); i++){

    for(var ii = 0; ii <= (Fields.length - 1); ii++){
        if(type[i].indexOf(Fields[ii]) == -1){
            FieldType = (Fields[ii]);
            break;
        };
    };

    for(var i=0; i <= (Req.length - 1); i++){
        if($('.more-options').is(':visible') && ($("" + FieldType + "[title=" +"'" + Req[i] + "'"+ "]").val() == "") )
        {
            ReqEmpty += 1;          
        };
    };
};



if(ReqEmpty == 0)
{
    return true;

}else if(ReqEmpty == 1){
    alert("There is 1 empty required field");
    return false;
}else{  
    alert("There are " + ReqEmpty + " empty required fields");
    return false;
};
};

HTML与SharePoint生成

HTML Generated with SharePoint

<table border="0" cellspacing="0" width="100%" class="input-table">
                <tr>
                    <td width="190px" valign="top" class="ms-formlabel">
                        <H3 class="ms-standardheader">
                            <nobr>Title<span class="ms-formvalidation"> *</span>
                            </nobr>
                        </H3>
                    </td>
                    <td width="400px" valign="top" class="ms-formbody">
                        <SharePoint:FormField runat="server" id="ff1{$Pos}" ControlMode="Edit" FieldName="Title" __designer:bind="{ddwrt:DataBind('u',concat('ff1',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Title')}"/>
                        <SharePoint:FieldDescription runat="server" id="ff1description{$Pos}" FieldName="Title" ControlMode="Edit"/>
                    </td>
                </tr>
                <tr>
                    <td width="190px" valign="top" class="ms-formlabel">
                        <H3 class="ms-standardheader">
                            <nobr>Selector<span class="ms-formvalidation"> *</span>
                            </nobr>
                        </H3>
                    </td>
                    <td width="400px" valign="top" class="ms-formbody">
                        <SharePoint:FormField runat="server" id="ff5{$Pos}" ControlMode="Edit" FieldName="Selector" __designer:bind="{ddwrt:DataBind('u',concat('ff5',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Selector')}"/>
                        <SharePoint:FieldDescription runat="server" id="ff5description{$Pos}" FieldName="Selector" ControlMode="Edit"/>
                    </td>
                </tr>
                <tr>
                    <td width="190px" valign="top" class="ms-formlabel">
                        <H3 class="ms-standardheader">
                            <nobr>Required Hidden 1</nobr>
                        </H3>
                    </td>
                    <td width="400px" valign="top" class="ms-formbody">
                        <SharePoint:FormField runat="server" id="ff2{$Pos}" ControlMode="Edit" FieldName="Required_x0020_Hidden_x0020_1" __designer:bind="{ddwrt:DataBind('u',concat('ff2',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Required_x0020_Hidden_x0020_1')}"/>
                        <SharePoint:FieldDescription runat="server" id="ff2description{$Pos}" FieldName="Required_x0020_Hidden_x0020_1" ControlMode="Edit"/>
                    </td>
                </tr>
                <tr>
                    <td width="190px" valign="top" class="ms-formlabel">
                        <H3 class="ms-standardheader">
                            <nobr>Required Hidden 2</nobr>
                        </H3>
                    </td>
                    <td width="400px" valign="top" class="ms-formbody">
                        <SharePoint:FormField runat="server" id="ff3{$Pos}" ControlMode="Edit" FieldName="Required_x0020_Hidden_x0020_2" __designer:bind="{ddwrt:DataBind('u',concat('ff3',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Required_x0020_Hidden_x0020_2')}"/>
                        <SharePoint:FieldDescription runat="server" id="ff3description{$Pos}" FieldName="Required_x0020_Hidden_x0020_2" ControlMode="Edit"/>
                    </td>
                </tr>
                <tr>
                    <td width="190px" valign="top" class="ms-formlabel">
                        <H3 class="ms-standardheader">
                            <nobr>Not Required Hidden 1</nobr>
                        </H3>
                    </td>
                    <td width="400px" valign="top" class="ms-formbody">
                        <SharePoint:FormField runat="server" id="ff4{$Pos}" ControlMode="Edit" FieldName="Not_x0020_Required_x0020_Hidden_" __designer:bind="{ddwrt:DataBind('u',concat('ff4',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Not_x0020_Required_x0020_Hidden_')}"/>
                        <SharePoint:FieldDescription runat="server" id="ff4description{$Pos}" FieldName="Not_x0020_Required_x0020_Hidden_" ControlMode="Edit"/>
                    </td>
                </tr>
                <tr>
                    <td width="190px" valign="top" class="ms-formlabel">
                        <H3 class="ms-standardheader">
                            <nobr>Did It Work</nobr>
                        </H3>
                    </td>
                    <td width="400px" valign="top" class="ms-formbody">
                        <SharePoint:FormField runat="server" id="ff6{$Pos}" ControlMode="Edit" FieldName="Did_x0020_It_x0020_Work" __designer:bind="{ddwrt:DataBind('u',concat('ff6',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Did_x0020_It_x0020_Work')}"/>
                        <SharePoint:FieldDescription runat="server" id="ff6description{$Pos}" FieldName="Did_x0020_It_x0020_Work" ControlMode="Edit"/>
                    </td>
                </tr>

                <tr id="idAttachmentsRow"><td nowrap="true" valign="top" class="ms-formlabel" width="20%"><SharePoint:FieldLabel ControlMode="Edit" FieldName="Attachments" runat="server" /></td><td valign="top" class="ms-formbody" width="80%"><SharePoint:FormField runat="server" id="AttachmentsField" ControlMode="Edit" FieldName="Attachments" __designer:bind="{ddwrt:DataBind('u','AttachmentsField','Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@Attachments')}" /><script>
      var elm = document.getElementById(&quot;idAttachmentsTable&quot;);
      if (elm == null || elm.rows.length == 0)
      document.getElementById(&quot;idAttachmentsRow&quot;).style.display=&apos;none&apos;;
    </script></td></tr>
                <xsl:if test="$dvt_1_automode = '1'" ddwrt:cf_ignore="1">
                    <tr>
                        <td colspan="99" class="ms-vb">
                            <span ddwrt:amkeyfield="ID" ddwrt:amkeyvalue="ddwrt:EscapeDelims(string(@ID))" ddwrt:ammode="view"></span>
                        </td>
                    </tr>
                </xsl:if>
            </table>

任何帮助将AP preciated

Any help would be appreciated

推荐答案

这可能不是这样做的最佳方式,但它确实工作。它使用类型arrTwo和所需物品的ArrOne。

This is probably not the best way of doing this, but it does work. It uses type for arrTwo and Req for ArrOne.

for(var i = (type.length - 1); i >= 0 ; i--){
    var SpliceVal = 0;

    for(var ii = 0; ii <= (Req.length -1); ii++){
        if(type[i].indexOf(Req[ii]) == -1){
            SpliceVal += 1;    
        };  
    };

    if(SpliceVal == Req.length){
        type.splice(i, 1);
    };
};

这篇关于使用数组来过滤数组的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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