How to filter out the repeat element from JS Array?

BadJohnny

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?

charlietfl

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to filter out values from array

From Dev

Angular JS - ng-repeat first element from an array of arrays

From Dev

Angular JS - ng-repeat first element from an array of arrays

From Dev

How group objects in array from ng-repeat with filter?

From Dev

Repeat data in array how to filter

From Dev

How to take random element from array without repeat and put it to textarea

From Dev

How to filter() out NaN, null, 0, false in an array (JS)

From Dev

How to filter from array elements in Vue JS

From Dev

How to filter out "Nothing" values from Elm Array?

From Dev

How to use swift flatMap to filter out optionals from an array

From Dev

How to filter out elements from an array that doesn’t match the query?

From Dev

How to filter out HTML tags from array and replace with nothing?

From Java

How to filter data from JS array and create another JS array

From Dev

How can I add an array to an object but filter out all but one column for each array element?

From Dev

How to filter autocomplete datas from array with 20 thousand element?

From Dev

How to filter by an array in AngularJs ng-repeat?

From Dev

How print array of object in ng-repeat with out using key.value in angular.js dynamically?

From Java

TypeScript filter out nulls from an array

From Dev

How to use golang to check repeat element in array?

From Dev

Angular.js filter by element of array

From Dev

Angular.js filter by element of array

From Dev

How can a filter out the array entries?

From Dev

Removing an element from an array with filter method.

From Dev

Filtering out objects with empty property from ng-repeat array

From Dev

How to filter an array of objects in js?

From Dev

How can I filter out non-Japanese characters from my array?

From Dev

How do I filter out non-int values from a double array using stream?

From Dev

How to filter out from a list of items

From Dev

How to filter out specific strings from a string

Related Related

  1. 1

    How to filter out values from array

  2. 2

    Angular JS - ng-repeat first element from an array of arrays

  3. 3

    Angular JS - ng-repeat first element from an array of arrays

  4. 4

    How group objects in array from ng-repeat with filter?

  5. 5

    Repeat data in array how to filter

  6. 6

    How to take random element from array without repeat and put it to textarea

  7. 7

    How to filter() out NaN, null, 0, false in an array (JS)

  8. 8

    How to filter from array elements in Vue JS

  9. 9

    How to filter out "Nothing" values from Elm Array?

  10. 10

    How to use swift flatMap to filter out optionals from an array

  11. 11

    How to filter out elements from an array that doesn’t match the query?

  12. 12

    How to filter out HTML tags from array and replace with nothing?

  13. 13

    How to filter data from JS array and create another JS array

  14. 14

    How can I add an array to an object but filter out all but one column for each array element?

  15. 15

    How to filter autocomplete datas from array with 20 thousand element?

  16. 16

    How to filter by an array in AngularJs ng-repeat?

  17. 17

    How print array of object in ng-repeat with out using key.value in angular.js dynamically?

  18. 18

    TypeScript filter out nulls from an array

  19. 19

    How to use golang to check repeat element in array?

  20. 20

    Angular.js filter by element of array

  21. 21

    Angular.js filter by element of array

  22. 22

    How can a filter out the array entries?

  23. 23

    Removing an element from an array with filter method.

  24. 24

    Filtering out objects with empty property from ng-repeat array

  25. 25

    How to filter an array of objects in js?

  26. 26

    How can I filter out non-Japanese characters from my array?

  27. 27

    How do I filter out non-int values from a double array using stream?

  28. 28

    How to filter out from a list of items

  29. 29

    How to filter out specific strings from a string

HotTag

Archive