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 How to remove duplicate records from string array

runfast

Coder
I have the following array:
Code:
myArray.push(name); \\Records that are being pushed: Record 1. name  = 10132, category = Computer Record 2. name =10132, category = Windows Server, Computer
myArray.push(category);
Code:
var myArray = [10132,Computer, 10132,Windows Server,Computer,10A8001HUS,Computer,10A8001HUS,Computer];

I need to remove the duplicate entries from this array.
The desired outcome:
Code:
var myArray = [10132,Computer, Windows Server,10A8001HUS,Computer];

Here is a code i have tried without any success:
Code:
for(var i=0;i<myArray.length;i++) {
    for(var j=i+1;j<myArray.length;j++) {
        if(myArray[i]===myArray[j]) {
            myArray.splice(j,1);
        }
    }
}
gs.log(myArray);

any suggestions or guidance greatly appreciated.
thanks
 
Last edited:
I did try with quotes and I am still getting duplicates when i did
gs.log(NEWA);
Code:
*** Script: 10132,Computer,Windows Server,Windows Server,Computer,10A8001HUS,Computer,Computer
 
I think splice() is the fastest way of doing this, but here's another way of doing it:
Code:
<script>
let  myArray = [10132,'Computer', 10132,'Windows Server','Computer','10A8001HUS','Computer','10A8001HUS','Computer'];
let newArry = [...new Set(myArray)];
alert(newArry);
</script>
Note the quotes!
 
I think splice() is the fastest way of doing this, but here's another way of doing it:
Code:
<script>
let  myArray = [10132,'Computer', 10132,'Windows Server','Computer','10A8001HUS','Computer','10A8001HUS','Computer'];
let newArry = [...new Set(myArray)];
alert(newArry);
</script>
Note the quotes!
Thank you for the tips! It seems that the issue is related to the data that is coming back from the table. I will keep looking.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom