0

I am trying to achieve the following result. I have a textarea, I want to transform all the text (by line) that will be input inside this textarea in an array. Once the text has been input, I want to check if there are duplicates in the entries.

To do so, I: 1) create an array (named myarrayfromtextarea, below) from the textarea 2) I create an array (named checkArray below) of the duplicates value 3) I loop through all the values in my first array and I check for matches in the second array.

Theoretically, it works well but I tried to input the following values in the textarea:

  • dwdw
  • dwdwdwdwdwdwdwdw
  • dwdwdwdw
  • dwdwdwdw
  • dwdwdwdwdwdwdwdwdwdw

I expected to find only one duplicate value (row 3 and 4) but incredibly, when I run --> checkArray.indexOf(array[i]) also the row 1 shows a match.

Can you help me?

var myarrayfromtextarea = $('#My_textarea').val().split('\n'); // create array from textarea
console.log("array from textarea: " + myarrayfromtextarea)


var myArr = myarrayfromtextarea; /// here I start to find duplicates
var obj = {};
var checkArray = "["
myArr.forEach(function(item) {
    if (typeof obj[item] == 'number') {
        checkArray = checkArray + item + ",";
        obj[item]++;

    } else {
        obj[item] = 1;
    }
});

checkarraylenght = (checkArray.length - 1)
checkArray = checkArray.substring(0, checkarraylenght)


checkArray = checkArray + "]" + '';
console.log("checkarray = " + checkArray)

myarrayfromtextarea = myarrayfromtextarea + '';
var array = myarrayfromtextarea.split(',');

var arrayLength = array.length;
for (var i = 0; i < arrayLength; i++) {

    if (checkArray.indexOf(array[i]) == -1) {
        console.log("Not a duplicate: " + array[i]);
    } else {

        console.log("Duplicate value: " + array[i]);
    }
    //Do something
}
Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98
Ob On Ken
  • 37
  • 1
  • 1
  • 8
  • 1
    `checkArray` is a string, and therefore you are using `String#indexOf` which returns partial matches, as opposed to `Array#indexOf` which only returns exact matches. There really is no point in the `checkArray` variable, though. You already create an object with the values - just iterate over the keys and see if the `obj[key] > 1`. If so, you know there are duplicates. – mhodges Aug 04 '17 at 15:21
  • I understand your explanation about the string / array but I am a bit lost on how to modify my code to make it works.. – Ob On Ken Aug 04 '17 at 15:35
  • See Taplar's solution. Use actual arrays, rather than string interpretations of arrays. – mhodges Aug 04 '17 at 15:52

3 Answers3

3

//maybe something like
var inputLines = $('textarea').val().split('\n');
var lineValues = {};

inputLines.forEach(function(line, index){
  if (!lineValues[line]) lineValues[line] = [];
  
  lineValues[line].push(index + 1);
});

Object.keys(lineValues).forEach(function(line){
  if (lineValues[line].length > 1) {
    lineValues[line].forEach(function(value){
      console.log(['Duplicate Value "', line, '":line:', value ].join(''));
    });
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea>
dwdw
dwdwdwdwdwdwdwdw
dwdwdwdw
dwdwdwdw
dwdwdwdwdwdwdwdwdwdw
</textarea>
<div><button>TEST</button></div>
Taplar
  • 24,788
  • 4
  • 22
  • 35
0

Quick and dirty using jQuery:

var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];
var uniqueNames = [];
$.each(names, function(i, el){
    if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
});
mhodges
  • 10,938
  • 2
  • 28
  • 46
0

This code returns all duplicated index!

ref: find duplication in string, textarea's lines

var a= [], j= -1, obj = $('textarea').val().split('\n');
for(var i in obj){
  j= -1;
  while((j= obj.indexOf(obj[i],j+1)) < 1) 
    a.push(i);
}
console.log(a)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<textarea rows="4" cols="50">
ok
ok.
nok
ok
</textarea>
easa
  • 306
  • 2
  • 9
  • Not exactly best practice to implicitly use the Array object with indexes as keys and use inline assignment inside of an expression. It is safe here, but as a general practice of writing JavaScript, this approach is frowned upon. – mhodges Aug 04 '17 at 16:23
  • @mhodges, I do not know where it may have a problem, may you explain more? – easa Aug 04 '17 at 17:11