JS array remove duplicates

There is an array of the form

['слово','слово ','новое слово',' новое слово','текст']

You need to get an array of the form

['слово','новое слово','текст']

That is, if the array elements can go with spaces both at the beginning and at the end of the word, you need to trim and remove duplicates from the array.

I remove duplicates like this

function onlyUnique(value, index, self) {
    return self.indexOf(value) === index;
}
var arr = tags.filter(onlyUnique);

But due to the fact that in the array, the second element comes with a space at the beginning of

'новое слово',' новое слово'

I get the same unfiltered array

Author: webphp, 2014-09-04

9 answers

From my scraps:

getUnique = function (arr) {
    var i = 0,
    current,
    length = arr.length,
    unique = [];
    for (; i < length; i++) {
      current = arr[i];
      if (!~unique.indexOf(current)) {
        unique.push(current);
      }
    }
    return unique;
  };

Note that this function does not change the original array, but returns a new one.

 4
Author: alvoro, 2014-09-04 20:19:33

UPD: If the data is curves, and you need to filter and normalize, then forEach:

function getUniqTags(tags) {
    var results = [];

    tags.forEach(function (value) {
        value = value.trim();

        if (results.indexOf(value) === -1) {
            results.push(value);
        }
    });

    return results; 
}

console.log(getUniqTags([' слово', 'слово', '  новое слово', 'новое слово  ', 'текст  ']));
 5
Author: RubaXa, 2014-09-05 10:13:49

@RubaXa em

var arr = tags.filter(function(value, index, self) {
    return self.indexOf(value.trim()) === index;
});

Updated:

tags.filter(function(value, index, self) {
    return self.join("#,#").replace(/#\s*|\s*#/g, '').split(',').indexOf(value.trim()) === index;
}).map(function(value, index, self) {
    return value.trim();
});
 3
Author: lampa, 2014-09-05 10:36:54

Alternatively, use reduce

function uniq (array) {
  return array.reduce((acc, current) => {
    current = current.trim();
    if (!acc.includes(current)) {
        acc.push(current);
    }
    return acc;
  }, []);
}

uniq(['слово','слово ','новое слово',' новое слово','текст']);
//["слово", "новое слово", "текст"]
 3
Author: Budimir, 2018-06-25 13:38:20
 2
Author: Pavel S. Zaitsau, 2014-09-05 08:27:19

You can also use this option

var tagsTrim = tags.map(function (str) {
   return str.trim();
});

var filterTags = tagsTrim.filter(onlyUnique);

function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}

It also seems to work =)

 2
Author: webphp, 2014-09-05 11:31:43

Simple method:

const arr = ['слово','слово ','новое слово',' новое слово','текст'];

const sortArr = arr.filter((it, index) => index === arr.indexOf(it = it.trim()));

console.log(sortArr) // [ 'слово', 'новое слово', 'текст' ]
 2
Author: DEN TRE, 2019-11-25 01:39:45
Array.from(new Set([1,2,3,4,5,5])) // [1,2,3,4,5]
[...new Set([1,2,3,4,5,5])]  // [1,2,3,4,5]
 2
Author: Maxim Nicolyuk, 2019-12-02 12:33:12
let arr = ['слово','слово ','новое слово',' новое слово','текст'];
let uniqArr = Array.from(new Set(arr.map(item=>item.trim())));
 1
Author: Spirit04eK, 2020-02-28 10:44:21