Home > Net >  Get uniq values using filter in JS
Get uniq values using filter in JS

Time:10-12

I want to display only uniq values when I'm using filter

For example, I have this:

const test = 'This is Right';
const test1 = 'This is wrong';
const test2 = 'This is Right';
const test3 = 'The word is hello'

const result = [test, test1, test2, test3].filter(value => value).join(' - ');
console.log(result);

The output is:

This is Right - This is wrong - This is Right - The word is hello

What I want :

This is Right - This is wrong - The word is hello

I already see some same issues but I don't wanna work with array. Thanks for your help

CodePudding user response:

You could use Set and initialize it with an array of strings

const test = 'This is Right';
const test1 = 'This is wrong';
const test2 = 'This is Right';
const test3 = 'The word is hello';

const m = new Set([test, test1, test2, test3])

console.log([...m].join(' - '))

CodePudding user response:

As you can see this is short version of @decpk

const test = "This is Right";
const test1 = "This is wrong";
const test2 = "This is Right";
const test3 = "The word is hello";

const result = [...new Set([test, test1, test2, test3])].join(" - ");
console.log(result);

As you can see this actually use array [test, test1, test2, test3] create a Set will delete duplicate then use join for create string.


You can use filter like:

const test = "This is Right";
const test1 = "This is wrong";
const test2 = "This is Right";
const test3 = "The word is hello";

const result = [test, test1, test2, test3].filter((value, index, self) => self.indexOf(value) === index).join(" - ");
console.log(result);

CodePudding user response:

You can use Set for achieving this-

const uniqueValues = new Set();
uniqueValues.add(test);
uniqueValues.add(test1);
uniqueValues.add(test2);
uniqueValues.add(test3);

console.log("result - " , uniqueValues);
console.log("Result in form of String : ", Array.from(uniqueValues).join(' - '));

// Or without using set:-

const values = [test, test1, test2, test3];
let result = [];
result = values.filter((item, index) => {
    return values.indexOf(item) === index;
});

console.log(result);

CodePudding user response:

1) You can get unique value using Set

const test = "This is Right";
const test1 = "This is wrong";
const test2 = "This is Right";
const test3 = "The word is hello";

const set = new Set().add(test).add(test1).add(test2).add(test3);
const result = [...set].join(" - ");
console.log(result);

2) Using Array literal with Set

const test = "This is Right";
const test1 = "This is wrong";
const test2 = "This is Right";
const test3 = "The word is hello";

const set = new Set([test, test1, test2, test3]);
const result = [...set].join(" - ");
console.log(result);

3) Use with Array only

const test = "This is Right";
const test1 = "This is wrong";
const test2 = "This is Right";
const test3 = "The word is hello";

const dict = [];
const result = [test, test1, test2, test3]
  .filter((value) => {
    if (!dict.includes(value)) {
      dict.push(value);
      return true;
    }
    return false;
  })
  .join(" - ");
console.log(result);

  • Related