1

I am having a recurring issue with arrays of strings I bring in from a spreadsheet. I want to filter a these arrays of strings for unique values and then fill options in a dropdown for a form.

I have tried multiple solutions, but for some reason none of them works.

function filterContainerDropdown(){

var containerFilterHelper =  inventory.filter(obj => obj.playerid == filterID)
    
  var containerOptions = containerFilterHelper.map(row => ({

      container: row.container,
     
    }));

    const unique = (x, i, a) => a.indexOf(x) == i;
     
    var uniqueContainers = containerOptions.filter(unique);

     console.log('')
     console.log('++++++++uniqueContainers+++++++++++')
     console.table(uniqueContainers); 

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

      setContainer[setContainer.length] = new Option(containerOptions[i].container,containerOptions[i].container)
        
    }

  }

I have tried solutions online galore. They work with other data. My data is a list of short strings.

EDIT: Are there well known issues with filtering strings for unique values?

EDIT2: The data I am giving it:

0   'Backpack'
1   'Backpack'
2   'Backpack'
3   'Backpack'
4   'Backpack'
5   'Backpack'
6   'Backpack'
7   'Backpack'
8   'Backpack'
9   'Backpack'
10  'Backpack'
11  'Backpack'
12  'Backpack'
13  'Backpack'
14  'Biltong'
15  'Biltong'
16  'Biltong'
17  'Biltong'
18  'Holding or Wearing'
19  'Holding or Wearing'
20  'Holding or Wearing'
21  'Holding or Wearing'
22  'Holding or Wearing'
23  'Holding or Wearing'
24  'Holding or Wearing'
25  'Holding or Wearing'
26  'Holding or Wearing'
27  'Holding or Wearing'
28  'Holding or Wearing'
29  'Holding or Wearing'
30  'Holding or Wearing'
31  'Holding or Wearing'
32  'Holding or Wearing'
33  'Holding or Wearing'
34  'Holding or Wearing'
35  'Holding or Wearing'
36  'Holding or Wearing'
37  'Holding or Wearing'
38  'Holding or Wearing'
39  'Holding or Wearing'
40  'Holding or Wearing'
41  'Location'
42  'Location'
43  'Location'
44  'Location'
45  'Location'
46  'Saddle Bag BL'
47  'Saddle Bag FL'
48  'Saddle Bag FR'
49  ''
50  ''
51  ''
52  ''
53  ''
54  ''
55  ''
56  ''
57  ''
58  ''
59  ''
60  ''
61  ''
62  ''
63  ''
64  ''

The data I want out:

 0  'Backpack'   
 1  'Biltong'   
 2  'Holding or Wearing'      
 3  'Location'        
 4  'Saddle Bag BL'
 5  'Saddle Bag FL'
 6  'Saddle Bag FR'
 7  ''
12
  • what is the input, and expected output that you are trying with the above func? Commented May 4, 2022 at 22:49
  • 1
    looks like container options holds objects which can't be found using indexOf you'll need to use findIndex. const unique = (x, i, a) => a.findIndex(({container})=> x.container === container ) === i; Commented May 4, 2022 at 22:55
  • 1
    But not with Objects... Commented May 4, 2022 at 22:58
  • 1
    For objects you need to extract the property that you want to make unique. Commented May 4, 2022 at 23:01
  • 1
    You're returning an obect from the map() instead just return the property var containerOptions = containerFilterHelper.map(row => row.container); Commented May 4, 2022 at 23:02

3 Answers 3

2

I think you can use the javascript new Set() method. As an example

let arr = ['Backpack','Backpack',  'Holding or Wearing',  'Holding or Wearing','','',''];
let unique = [...new Set(arr)];

console.log(unique) // ['Backpack', 'Holding or Wearing', '']
Sign up to request clarification or add additional context in comments.

Comments

1

You made this more complicated than it has to be. You can simplify it to something like this:


function filterContainerDropdown(){
  const values = inventory.filter(({ playerid }) => playerid == filterID)
                   .map(({ container }) => container)
                   .filter((x, i, a) => a.indexOf(x) == i)
                   .map((v) => new Option(v, v));
  setContainer.push(...values);
}

The problem with your approach is that you're running the function that is supposed to filter out duplicates on an array that contains objects. This will filter out duplicate objects in the array not duplicate strings.

Here, I'm mapping the array of objects to an array of strings (..map(({ container }) => container)). This way you'll get the required result.

Comments

0

You're returning an obect from the map() instead just return the property

var containerOptions = containerFilterHelper.map(row => row.container);

– pilchard

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.