0

I use javascript to get response data from firebase result. Now I have list item respone like

Luxury
Sedan
MPV
Hatchback
SUV
Luxury
Luxury
Luxury
Luxury

Now I want to display this respone on HTML, but no have duplicate more name luxury.

I try to use

var uniqueNames = [];
            $.each(names, function(i, el){
                if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
            });

With names is my list respone, but it not a array. What I wrong in here, please help me to find a way to fix it

Thanks a lot

you can follow data this image link image response here

2 Answers2

0

Time to switch to Set from ES6

Set objects are collections of values. You can iterate through the elements of a set in insertion order. A value in the Set may only occur once; it is unique in the Set's collection

const uniq = new Set(['Luxury',
  'Sedan',
  'MPV',
  'Hatchback',
  'SUV',
  'Luxury',
  'Luxury',
  'Luxury',
  'Luxury'
])

for (let item of uniq.keys()) console.log(item)
Krzysztof Safjanowski
  • 7,292
  • 3
  • 35
  • 47
0

I not sure whats the problem on your situation, maybe you can provide the response data from database and the end result you get. We might figure out wheres the problem located.

But here is an alternative way to do it.

var uniqueNames = names.filter(function(elem, index, self) {
     return index == self.indexOf(elem);
})

Hope it help.

Dean
  • 668
  • 12
  • 32