0

Below is my code to load the combo using a txt file:

$("#Combo1").change(function() {
    $('#Combo2').empty();
                $.ajax({
                url: 'File.txt',
                type: 'get',
                success: function(txt){
                    var value = [];
                    var txtArray = txt.split('\n');
                    for (var i = 0; i < txtArray.length; i++)
                    {
                        var tmpData = txtArray[i].split(',');
                        if (!value[tmpData[1]]) 
                        {
                        value.push([tmpData[1]]);
                        value[tmpData[1]] = true;
                        }
                    }
                    $('#Combo2').empty();
                    $.each(value, function(i, p) {
                    $('#Combo2').append($('<option></option>').val(p).html(p));
                    });

                }
                })
            });
    $("#Combo1").trigger("change");

Here on change of Combo1, this will be called. The Ajax is used to read the content of File.txt File.txt has a "," seperated two columns, out of which I am willing to print coulmn2. Given below are the contents of File.TXT.

A,31JAN
B,25JAN
C,31JAN
D,6JAN
E,6JAN

I was to load the above dates in Combo2. With the above code, I am able to ignore 31JAN. But 6JAN is getting repeated. In short, the value which is given at the last row gets repeated. Rest is fine.

Programmer
  • 329
  • 2
  • 6
  • 25
  • I have further debugged the same and got to know that the last row when stored in array, gets stroed without double quotes. Anything to do with that? – Programmer Jan 24 '19 at 01:28

1 Answers1

1

Try this:

var txt="A,31JAN\nB,25JAN\nC,31JAN\nD,6JAN\nE,6JAN";
var txtArray = txt.split('\n');
for (var i = 0; i < txtArray.length; i++)
       txtArray[i] = txtArray[i].split(",").pop();
var value = txtArray.reduce(function(a,b){if(a.indexOf(b)<0)a.push(b);return a;},[]);
console.log(value); //returns array 

Also, give this a read: https://stackoverflow.com/a/9229821/9920079 :)

Hackinet
  • 3,252
  • 1
  • 10
  • 22
  • Above code return both the columns. When I check two columns, those are anyways unique. I want the uniqueness of column 2 only. – Programmer Jan 24 '19 at 01:10
  • @Programmer The above code(my answer) returns unique elements for column 2?? – Hackinet Jan 24 '19 at 10:04
  • I found the solution for my problem. The above code was returning column 1 as well as 2. I wanted only column 2. In by file I have added a ,delimiter at the end of column 2 as well. That has solved my issue, thanks. – Programmer Jan 24 '19 at 12:52