I have two JS arrays like below:
$order = [];
$a = ['about','about','portfolio','contact'];
Get the order of items:
for($i=0;$i<$a.length;$i++){
$order.push($i+1);
}
console.log($order);
Output: [1,2,3,4]
But I want to filter out the repeat items and got the order like [1,3,4]
Another example
$a = array('about','about','about','portfolio','contact','contact');
I want to get the order like [1,4,5]
I'm not sure whether I explained clear. Can anyone give me a suggestion?
Can use a Set
to store unique values and Array#map()
to return positions based on Array#indexOf()
const arr = ['about', 'about', 'about', 'portfolio', 'contact', 'contact']
const res = [...new Set(arr)].map(v => arr.indexOf(v) + 1)
console.log(res)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments