2014-08-27 330 views
0

我想查看一個數組中的每個值,看看它們中的任何一個是否包含在任何其他數組值中。 例如:使用數組來過濾一個數組Javascript

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

所以我希望它循環看到arrTwo每個值有從arrOne的一個值,並刪除不要讓那些我得到:

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

這裏我與工作代碼:

的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

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

任何幫助,將不勝感激

+0

正是你想要在有這麼大的代碼是什麼? – Mritunjay 2014-08-27 13:54:27

+0

這是什麼問題?你已經有了代碼。 – 2014-08-27 13:54:41

+0

這是一個SharePoint列表表單列的自定義驗證,使一些需要。我需要查看哪些列是必需的,並且我已經在數組Req中預定義了這些列。但我需要查看哪些表單元素包含所需列的標題或數組Req中的內容 – 2014-08-27 14:00:44

回答

0

這可能不是這樣做的最好的方式,但它確實工作。它使用arrTwo的類型和ArrOne的Req。

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); 
    }; 
}; 
0

有兩種可能的方法來解決這個問題,這取決於你如何佈局陣列。

選擇一 - 假定兩個陣列將具有匹配的值的結構

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

$(document).ready(function() { 
    $.each(arrOne, function (key, value) { 
     var index = $.inArray(value, arrTwo); 
     if (index != -1) { 
      arrFinal.push(arrTwo[index]); 
      console.log(value); 
     } 
    }); 
}); 

選擇二 - 假定兩個陣列將不具有匹配的值的結構(你上面的例子)

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

$(document).ready(function() { 
    $.each(arrOne, function (key1, value1) { 
     $.each(arrTwo, function (key2, value2) { 
      var index = value2.indexOf(value1); 
      if (index != -1) { 
       arrFinal.push(value2); 
       console.log(value2); 
      } 
     }); 
    }); 
}); 
0

只要環通過並添加它發現的一個新的數組。不需要拼接。

function result(arr1, arr2) { 
    var out = []; 
    for (var i = 0, l = arr2.length; i < l; i++) { 
     var el2 = arr2[i]; 
     var rel2 = el2.replace('option ', ''); 
     if (arr1.indexOf(rel2) > -1) { out.push(el2); } 
    } 
    return out; 
} 

result(arr1, arr2); 

DEMO

0

如果你可以使用一個第三方庫,你可以使用underscorejs,它提供了大量的實用功能。

function customFilter(arrOne, arrTwo) { 
    return _.filter(arrTwo, function(item){ 
     var res = item.substring('option '.length, item.length); 
     return _.contains(arrOne, res); 
    }); 
} 

var result = customFilter(arrOne, arrTwo); 

DEMO

0

的JavaScript

Array.prototype.Filter = function(filters) 
{ 
    var t = new Array(); 
    for(var i = 0; i < this.length; ++i) 
    { 
     for(var j = 0; j < filters.length; ++j) 
     { 
      if(this[i].contains(filters[j])) 
      { 
       t.push(this[i]); 
      } 
     } 
    } 
    return(t); 
}; 

/* Exemple */ 
var filters = [ 'a', 'b' ,'c' ]; 
var arr = [ 'option a', 'option c', 'option b', 'option d' ]; 
var result = arr.Filter(filters); 
alert(result);