使用一个字符串数组来拼接另一个数组?

时间:2016-04-07 08:20:24

标签: javascript jquery arrays

所以我有一个数组:

var array1 = ['one', 'two', 'three', 'four', 'five']

另一个:

var array2 = ['two, 'four']

如何从array2中删除array1中的所有字符串?

4 个答案:

答案 0 :(得分:4)

只需使用Array#filter()Array#indexOf()bitwise not <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="test()"/> <script type="text/javascript"> function test() { document.getElementById("<%=Label2.ClientID%>").innerText = document.getElementById("<%=TextBox1.ClientID%>").value; //Somthing like this } </script> 运算符进行检查。

  

<svg width="500" height="240" version="1.1" xmlns="http://www.w3.org/2000/svg"> <defs> <linearGradient id="Gradient2" x1="16%" x2="1%" y1="50%" y2="50%"> <stop offset="0%" stop-color="#c0c0c0"/> <stop offset="20%" stop-color="#c0c000"/> <stop offset="40%" stop-color="#c0c000"/> <stop offset="60%" stop-color="#c000c0"/> <stop offset="80%" stop-color="#c00000"/> <stop offset="100%" stop-color="#0000c0"/> </linearGradient> <linearGradient id="repeat"xlink:href="#Gradient2"spreadMethod="repeat" /> <style type="text/css"><![CDATA[ #rect1 { fill: url(#repeat); } .stop1 { stop-color: #c0c0c0; } .stop2 { stop-color: #c0c000; } .stop3 { stop-color: #c0c000; } .stop4 { stop-color: #c000c0; } .stop5 { stop-color: #c00000; } .stop6 { stop-color: #0000c0; } ]]></style> </defs> <rect id="rect1" x="10" y="10" rx="15" ry="15" width="120" height="30"/> bitwise not operator。它非常适合与indexOf()一起使用,因为~如果找到索引~则返回,如果不是indexOf则返回:

0 ... n

-1

答案 1 :(得分:2)

试试这个。

array2.forEach(item => array1.splice(array1.indexOf(item),1));

答案 2 :(得分:0)

使用inArray方法在jquery中

for(array1)
  var idx = $.inArray(array1[i], array2);
  if (idx != -1) {//-1 not exists 
     array2.splice(idx, 1);
  } 

}

答案 3 :(得分:0)

&#13;
&#13;
var array1 = ['one', 'two', 'three', 'four', 'five']
var array2 = ['two', 'four']
    
array1 = array1.filter(function(item){
   return array2.indexOf(item) === -1
})
// ['one', 'three', 'four', 'five']

document.write(array1)
&#13;
&#13;
&#13;

相关问题