I have a select where i can get some values as array like ["1","2","3"]
On every change i run the following code to get a result of array which is connected to these values i get from select
$('#event-courses-type').on('change',function(){
type = $('#event-courses-type').val();
console.log(type);
var var1 = ["4689 Leadership Award", "UKCC Level 1", "UKCC Level 2", "UKCC Level 3", "UKCC Level 4", "Old WHU Award", "None at present"];
var var2 = ["4689 Leadership Award", "GB-wide Level 1", "Old Level 1 (Pre January 2012)", "Level 2", "Level 3", "EHF", "FIH", "None at present"];
var var5 = ["D32/33", "A1 Assessor", "CTS", "IAPS", "PTLLS", "AVRA", "Umpire Educator Training", "Umpire Assessor Training"];
var var6 = ["D32/33", "A1 Assessor", "CTS", "IAPS", "PTLLS", "AVRA", "Umpire Educator Training", "Umpire Assessor Training"];
var results = [];
if ($.inArray("1",type) != -1) {
var results = $.merge(var1, results);
}
if ($.inArray("2",type) != -1) {
var results = $.merge(var2, results);
}
if ($.inArray("5",type) != -1) {
var results = $.merge(var5, results);
}
if ($.inArray("6",type) != -1) {
var results = $.merge(var6, results);
}
console.log(results);
)};
Here is my console log to let you see the type array after i selected 2 options and the results array:
["1", "2"] ------------------- add_event.php:802
["4689 Leadership Award", "GB-wide Level 1", "Old Level 1 (Pre January 2012)", "Level 2", "Level 3", "EHF", "FIH", "None at present", "4689 Leadership Award", "UKCC Level 1", "UKCC Level 2", "UKCC Level 3", "UKCC Level 4", "Old WHU Award", "None at present"]
As you see there is two times "4689 Leadership Award" and i dont want this to happen. In PHP i use the array_unique()
function to eliminate these duplicated values but i dont know how to do it in jQuery.
just use unique() method.
Using each() check value of inputs and if any value is duplicate add class duplicate to it.
The array_unique() function removes duplicate values from an array.
The $. unique() function searches through an array of objects, sorting the array, and removing any duplicate nodes. A node is considered a duplicate if it is the exact same node as one already in the array; two different nodes with identical attributes are not considered to be duplicates.
try this:
function unique(array){
return array.filter(function(el, index, arr) {
return index == arr.indexOf(el);
});
}
working example:
const startingArray = [1, 2, 2, 3, 2, 3, 3, 3];
function unique(array){
return array.filter(function(el, index, arr) {
return index == arr.indexOf(el);
});
}
const uniqueArray = unique(startingArray);
console.log(uniqueArray);
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