2

I have an array in JavaScript like this

var data = [,A_1_VII,VII,V2,,A_1_VII,VII,V2,,A_1_VII,VII,V2,,B_1_XIV,XIV,V3,,B_2_XVI,XVI,V3]   

when I alert in JavaScript it gives as below


,A_1_VII,VII,V2
,A_1_VII,VII,V2
,A_1_VII,VII,V2
,B_1_XIV,XIV,V3
,B_2_XVI,XVI,V3

But I want like this which is duplicates removed array

var unique_data = [,A_1_VII,VII,V2,,B_1_XIV,XIV,V3,,B_2_XVI,XVI,V3]

On alert it should give like this

,A_1_VII,VII,V2
,B_1_XIV,XIV,V3
,B_2_XVI,XVI,V3

16
  • What have you tried so far? Also your current JS example will throw an error. Commented Nov 14, 2017 at 10:27
  • 1
    That is a really strange javascript array. Is this code valid javascript? Commented Nov 14, 2017 at 10:27
  • 2
    @JasperSeinhorst it is, if those are valid variables. Commented Nov 14, 2017 at 10:28
  • 1
    @DontVoteMeDown I doubt that variable names can start with a comma. Commented Nov 14, 2017 at 10:29
  • 4
    @BojanIvanac: No, but arrays can have empty entries in them, which is what that array has. (This is as of ES2015+) For example: [,,,1,,,] is perfectly valid (in ES2015+). Commented Nov 14, 2017 at 10:32

3 Answers 3

1

First Thing your array contains string as a constant that's not going to work.

Secondly, if all of you value are strings you can do it as follows:

var data =[,"A_1_VII","VII","V2",,"A_1_VII","VII","V2",,"A_1_VII","VII","V2",,"B_1_XIV","XIV","V3",,"B_2_XVI","XVI","V3"];

var uniqueArray = data.filter(function(item, pos) {
    return data.indexOf(item) == pos;
})

alert(uniqueArray);

Sign up to request clarification or add additional context in comments.

2 Comments

Not working man. I tried this already. Now also again I tried. But not working.
@bioinform did you put all your values into double quotes, as above codes are working fine click on "Run code snippet"
1

Assuming the variables in your array are well defined, you can clean it up and remove duplicates with a for loop:

var data [/* ... */];
var unique_data = [];

for(let i = 0; i < data.length; i++) {
    if (data[i] && unique_data.indexOf(data[i]) === -1) {
        unique_data.push(data[i]);
    } 
}

Please note that the code above assumes that your array contains non-object types, otherwise the solution would need to use something more sophisticated than indexOf().

Comments

1

You can create your unique function to remove duplicate entry and empty value from array like this.

var data =[,"A_1_VII,VII","V2,,A_1_VII","VII","V2",,"A_1_VII","VII","V2",,"B_1_XIV,XIV","V3",,"B_2_XVI,XVI,V3"]   
var unique_data = uniqueList(data);
alert(unique_data);
function uniqueList(list) {
  var uniqueResult = [];
  $.each(list, function(i, e) {
    if ($.inArray(e, uniqueResult) == -1 &&$.inArray(e, uniqueResult)!="")// chech for unique value and empty value
      uniqueResult.push(e);
  });
  return uniqueResult ;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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.