my script creates an empty array and then fill it. But if new arguments come then script is expected to destroy old one and create new one.
var Passengers = new Array();
function FillPassengers(count){
for(var i=0;i<count;i++)
Passengers[i] = i;
}
I wanna destroy the old one because new count may be less than old one and last elements of array still will store old array? is that right and if it is how can I destroy it?
To remove the duplicates from an array of objects:Use the Array. filter() method to filter the array of objects. Only include objects with unique IDs in the new array.
To find a unique array and remove all the duplicates from the array in JavaScript, use the new Set() constructor and pass the array that will return the array with unique values.
To remove duplicates from an array: First, convert an array of duplicates to a Set . The new Set will implicitly remove duplicate elements. Then, convert the set back to an array.
Answer: Use the indexOf() Method You can use the indexOf() method in conjugation with the push() remove the duplicate values from an array or get all unique values from an array in JavaScript.
You can simply do this to empty an array (without changing the array object itself):
Passengers.length = 0;
Or, with your code:
function FillPassengers(count){
for(var i=0;i<count;i++)
Passengers[i] = i;
Passengers.length = count;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With