Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript 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

Code:
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){
        
        namesArray.push(product_name);
    }
}

console.log(namesArray);

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

Desired output is
['name-3', 'name-4', 'name-5']
 
There are a few things worth considering...
  • You must track duplicates even if the name before it is not the same. A duplicate may be separated by another name in a real world scenario when names aren't alphabetically sorted.
  • The reason you are getting the duplicate names is because your code is not adding a name when it's a duplicate, but the "first" duplicate is still allowed to stay in the array (ex: the first name-1 goes into array but never gets removed when duplicate name-1s show up)
  • You must track the duplicates as you are going through and then filter the excluded names so your final result is a duplicate-free list of names.
Code:
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 = [],
    excludeNames = [];

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);
});

console.log(filtered);
namesArray = filtered;
 
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.

Code:
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;

}   

fs.createReadStream('spreadsheets/output.csv')
  .pipe(parse({ delimiter: ',' }))
  .on('data', (r) => {
    data.push(r); 
  })
  .on('end', () => {
 
    for(var i = 0; i < data.length; i++){

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

colorArray.push([product_name]);

flatArray = [...colorArray];
console.log(flatArray);

    }}
  )
 
Here is what the input file, unfortunately named output.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"
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom