-1

Here I have two multiline textboxes and enter some data in two textboxes like txt1 1,1,1,2,2,2,3 txt2 1,2,3,4,5, when i press Filter button it shows the data in thirdtextbox like txt3 4,5 (both the textboxes are having these numbers) My snippet is

<script type="text/javascript">
    function GetDistinctElements(source,source1, target) {           
        var input = source.value.trim().replace(';', ',').split(',');
        var input1 = source1.value.trim().replace(';', ',').split(',');           
        var Array = input.concat(input1);
        var distinctArray = Array.filter(function (item, pos) {              
            return Array.indexOf(item) == pos;
        });           
        target.value = distinctArray.join(',');
    }
</script>

and my controls are

<table border="0" align="left">
    <tr>
        <td>Enter Numbers:</td>
        <td> <asp:TextBox ID="txt1" TextMode="MultiLine" runat="server"></asp:TextBox></td>
        <td>Enter Numbers:</td>
        <td><asp:TextBox ID="txt2" TextMode="MultiLine" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td><input type='button' value='Get Distinct Items' onclick='GetDistinctElements(<%= txt1.ClientID %>,<%= txt2.ClientID %>,<%= txt3.ClientID %>); return false;' /></td>
        <td><asp:TextBox ID="txt3" TextMode="MultiLine" runat="server"></asp:TextBox></td>
    </tr>
</table>

when I tried this snippet, I didn`t get the expected output.

4
  • stackoverflow.com/questions/4343746/… Commented Jan 25, 2015 at 6:48
  • stackoverflow.com/questions/2523436/… Commented Jan 25, 2015 at 6:49
  • "I didn`t get the expected output." - You could be a bit clearer about what the expected output actually is. And did you get some other output instead, or an error in your browser's dev console? Commented Jan 25, 2015 at 6:58
  • txt1: 1,1,1,2,2,2,3 txt2: 1,2,3,4,5, when i press Filter button it shows the data in thirdtextbox like txt3: 1,2,3 (both the textboxes Commented Jan 25, 2015 at 7:03

2 Answers 2

0

Replace this:

var Array = input.concat(input1);
var distinctArray = Array.filter(function (item, pos) {              
    return Array.indexOf(item) == pos;
});           

by this:

var input2 = input.concat(input1);
input2.sort(function(a,b){return a-b});
var prev= null;
var distinctArray = input2.filter(function (item, pos) {              
    if (item == prev){
      return false;
    }
    prev = item;
    return true;
});           
Sign up to request clarification or add additional context in comments.

2 Comments

,thanks for your reply...but it shows two textboxes values in third textbox like t1:1,2,3.t2:1,3,4,5 t3:1,2,3,1,3,4,5
fixed that with adding input2.sort();
0

I got the Answer

 function GetDistinctElements(source,source1, target) {
        // Get the items in your input
        var input = source.value.trim().replace(';', ',').split(',');
        var input1 = source1.value.trim().replace(';', ',').split(',');           
        input1 = input1.filter(function (val) {
            return input.indexOf(val) == -1;
        });
        // Output the result in  target area
        target.value = input1.join(',');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.