An Array of Different Names

I have an array of names and I want to push only the different names to the namesArray. So this code should output only name-3, name-4, and name-5

var names = ["name-1","name-1","name-1","name-1","name-2","name-2","name-3","name-4","name-5","name-6","name-6"];

var namesArray = [];
for(var i = 0; i < names.length; i++){
product_name = names[i];
product_name_compare = names[i-1];

	if (product_name != product_name_compare){


This is the output:
[ ‘name-1’, ‘name-2’, ‘name-3’, ‘name-4’, ‘name-5’, ‘name-6’ ]

I would suggest keeping a running total of each name encountered while iterating through the array and then create a new array with the names that only had a count of 1.

1 Like

Do you have a question?

Your current solution will only work if names is a sorted list, but if you know that it will always be sorted, that’s fine.

The code that I needed is part of a larger project. In this project, I loop through a CSV file and output only the product names that have no variants. This is what the project looks like but I am getting the wrong output.

const parse = require('csv-parse');
const fs = require('fs'); 
const data = [];
	var names = [];
	var colorArray = [];
	var namesArray = [];
	var excludeNames = [];
function getDifferentNames(names) {
for(var i = 0; i < names.length; i++){
    if(!namesArray.includes(names[i])){ // if not in array:
        namesArray.push(names[i]); // add to array
    } else {  // already in array:
        excludeNames.push(names[i]); // add to our exclude list

// now we must remove our exclude list from the names list:
var filtered = namesArray.filter(function(value, index, arr){
    return !excludeNames.includes(value);
return filtered;


  .pipe(parse({ delimiter: ',' }))
  .on('data', (r) => {
  .on('end', () => {
	for(var i = 0; i < data.length; i++){

product_name = getDifferentNames([data[i][0]]);


flatArray = [...colorArray];


This is what the input CSV looks like:

“Freshwater Pearl Disc Beaded Anklet”,
“Bead Link Layered Anklet”,
“4pcs - Sexy Rhinestone Pave Chain Layered Anklets”,
“4pcs - Sexy Rhinestone Pave Chain Layered Anklets”,
“4pcs - Baby Rhinestone Pave Chain Layered Anklets”,
“4pcs - Baby Rhinestone Pave Chain Layered Anklets”,
“Rhinestone Pave Sexy Anklet”,
“Rhinestone Pave Sexy Anklet”,
“Rhinestone Pave Boss Anklet”,
“Rhinestone Pave Glam Anklet”,
“Rhinestone Pave Glam Anklet”

The output should be: “Freshwater Pearl Disc Beaded Anklet”, “Bead Link Layered Anklet”, “Rhinestone Pave Boss Anklet”

With the code above, can you log namesArray?

When I console.log namesArray, it returns all of the names not just the ones I want to return. This is the expected behaviour.

If I am understanding correctly, you are trying to create an array that contains only the names that appear only once in your original array. Your getDifferentNames function should do that. Here is your function with simple input:

If no elements are being removed from your names then the items are not “equal” in a JavaScript sense. Are they objects? You cannot compare objects for equality. If they’re just strings, there must be some variation in the strings. I’m assuming that you have narrowed down your problem to the getDifferentNames function.

The input CSV is just strings.

Thanks ArielLeslie and RandellDawson for your input.

There’s some flaws in your script., you’re just fortunate that your names array doesn’t have the property ‘-1’.

Otherwise this line would cause undesirable results:

product_name_compare = names[i-1];

When referencing negative indexes in Javascript it simply looks for the property of that object. Doesn’t work like Python.

As for removing duplicates. That’s easy:

const my_list = ["name-1","name-1","name-1","name-1","name-2","name-2","name-3","name-4","name-5","name-6","name-6"];
console.log(new Set(my_list));

However, instead of just building an array then creating a Set from it, just insert it into a Set to begin with.

As for ArielLeslie comment about comparing objects, there’s exceptions to the rule. If there’s no references as the key or value you can compare objects for equality.

Thanks kerafyrm but you’re referencing a script that I don’t use any more.

1 Like

Even in your 2nd code example. You’re iterating over same data multiple times.

@kerafyrm, I don’t understand what you mean.

Is your 2nd code example the script you’re now using?

Yes, my 2nd code example is the script I’m now using.

Yeah - several problems:

product_name = getDifferentNames([data[i][0]]);

Implies your getDifferentNames function is returning something. And if you look at that function, there’s no returns. All functions return undefined if not explicit.

But that’s a small issue.

If ultimately you’re just trying to get a list with no repeats you only need to do:

const uniqueProducts = data.reduce((a, b) => {
	let [ products ] = b;
    return new Set([...a, ...products]);
}, new Set());

But this is implying your current code is identifying your data correctly.

I’m assuming it’s something like:

const data = [[['apple', 'banana', 'apple', 'cherry']], [['apple', 'banana', 'pinapple', 'cherry']], [['apple', 'banana', 'apple', 'cherry']]];

My getDifferentNames function returns filtered.

I’m not just trying to get a list with no repeats. I’m trying to output only the elements that are non-variant. Thanks anyway.