Introduction:
I would like to say this article going to help most of the developers who are in need to remove duplicates which are present in Original array by comparing with the other array elements. If you could not understand what I am saying, look at the below example you will get an idea on how we can play with JavaScript built-in functions.
Actual Task:
My actual task is, I have two arrays one naming Array1 and other naming Array2. In Array1 I have elements like “a”,”b”,”c”,”d”,”e”,”f” and in Array2 I have elements like “c”,”e” only. Now, I want to remove elements “c” and “e” from Array1 (by comparing Array2), hope you have understood now whats going to be the output yeah! the output should be Output: “a”,”b”,”d”,”f”. This can be achieved using concat and slice functions. Want, to know how we can achieve this, see below for respective code snippets:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <script language="javascript"> var Array1 = new Array("a","b","c","d","e","f"); var Array2 = new Array("c","e"); for (var i = 0; i<Array2.length; i++) { var arrlen = Array1.length; for (var j = 0; j<arrlen; j++) { if (Array2[i] == Array1[j]) { Array1 = Array1.slice(0, j).concat(Array1.slice(j+1, arrlen)); }//if close }//for close }//for close alert(Array1); </script> |
Hope this might help some of the developers who are looking for the same logic. Please, let me know if you have any suggestions in making it more compatible enough by reducing lines of code.
Related Entries...
Here is a simple and easily understandable snippet code, to remove duplicates from an existing array ...
Introduction: This article will helps you in removing any duplicate values from Multi Dimensional A ...
Introduction Recently, one of my reader came up with a query like "Why JavaScript is not running aft ...
In some scenarios we will have catch hold of number of occurrences while coding, like for example: In ...
I am back again with some helpful snippet code. Here using JavaScript we can pro actively sort date f ...
In the following article we can have a look at the ways of creating new files in Ruby programming lan ...
'How to select elements which are having multiple classes using jQuery', this question has raised whe ...
This article will help beginners, that is who are new to jQuery and want to learn some tricks about j ...
Do you want to know and detect visitor’s browser version using JavaScript, yeah here is the script th ...
All lovable people of C++, here are sample snippet code which you guys can make use. I want to say o ...























7 Responses
[...] This post was Twitted by niceoutput – Real-url.org [...]
Nice snippet, Vivek! I know this is useful because Vivek utilized this concept on our team’s recent web application project.
Keep up the good work!
Hi Daniel,
Nice to see your comment here, thank you verymuch!
Thanks,
Vivek
woah! thanx a lot dude! it really helps!
Hi Joe,
Thanks a lot for your valuable comments, it really helps me in posting some new articles like this, which going to help everyone in all aspects. cheers… keep tunning into this site, its really going to help you a lot….
Thanks,
Vivek
Realy valuable code snipptes and smart too. Thanks for sharing.
hi there
and if there is an element in Array2 that is not present in array1??
how can i output ALSO array2 with the elements that dont match??
thx