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
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']
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']