你可以删除从另外一个阵列中的JavaScript或jQuery的 [英] can you remove one array from another in javascript or jquery

查看:103
本文介绍了你可以删除从另外一个阵列中的JavaScript或jQuery的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JavaScript中的三个数组

i have a three arrays in javascript

var array1 = new Array (1,2,3,4,5);
var array2 = new Array ("a", "b", "c", "d", "e");
var array3 = new Array ("a", "c", "d");

和我基本上要:


  1. 创建与ARRAY3减去数组2项新的数组。因此它应导致

  1. Create a new array with array2 minus items in array3. So it should result in

VAR array4 =新的Array {B,E};

var array4 = new Array {"b", "e"};

创建数组1,与阵列4对齐的相应指标另一个数组,所以在这种情况下,我也想能够产生

Create another array with the corresponding index of array1 that aligns with array 4, so in this case i would also want to be able to generate

VAR array5 =新的Array {2,5}

var array5 = new Array {2, 5}

我知道DOTNET 3.5有很多简单的方法,要做到这一点操作,但不知道,如果JavaScript从一开始类似的事情。

i know in dotnet 3.5 there are lots of simple methods to do this operation but wasn't sure if javascript had anything similar.

推荐答案

好吧,首先,数组的声明采取两种形式:

Ok, for starters, array declaration takes one of two forms:

var myArr = new Array(1, 2, 3);
// or
var myArr = [1, 2, 3];

第二个选项是更好的,顺便说一句。 <一href=\"http://stackoverflow.com/questions/931872/whats-the-difference-between-array-and-while-declaring-a-javascript-arr\">See这里原因。

你实际上寻找的是一种叫差(相对于相交或联合)。有堆栈溢出的一些其他问题有关数组的区别,像这样的,但是接受的解决方案使用的。在的数组,这是不是一个好主意上。我听到道格·克罗克福德字面上猛击你的脸的通过互联网的每一个你做到这一点的时间。 这里有更多的技术讨论一下,如果你有兴趣的。

What you're actually looking for is something called difference (as opposed to intersection or union). There's some other questions on Stack Overflow about array difference, like this one, however the accepted solution uses for .. in on an array, which is not a good idea. I heard Doug Crockford literally punches you in the face through the Internet every time you do that. Here's a more technical discussion about it if you're interested.

答案<一个href=\"http://stackoverflow.com/questions/1723168/what-is-the-fastest-or-most-elegant-way-to-compute-a-set-difference-using-javascr\">this问题可能更适合你。

这篇关于你可以删除从另外一个阵列中的JavaScript或jQuery的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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