TLDR;
Is there any way to use $.inArray by only knowing what the element begins with or contains?
I have an array of elements and I would like to be able to pick out an element that begins with a certain string as the value is dynamic.
For example:
My array is:
array = ["Alpha_GB8732", "Beta_GB29834", "Gamma_GB2384", "Delta_GB23984"]
But for each user, the order and the exact names may change.
Use jQuery, I can do the following to get the element if I know the exact value.
var order = $.inArray("Alpha_GB8732", array)
$item = $(array).get(order)
However, since everything after GB changes for each element on the page load, I cannot use this as I do not know the exact string.
Is there any way to use $.inArray by only knowing what the element begins with or contains?
ANSWER
Thanks so much for all of your help guys! I really appreciate it.
In the end, the best answer for me was Louis's response:
let array = ["Alpha_GB8732", "Beta_GB29834", "Gamma_GB2384", "Delta_GB23984"]
let result = array.filter((el) => el.includes("Alpha_GB") === true);
console.log(result);
This didn't work for me at first... because gulp-uglify for some reason couldn't compile the es6 script. So, just for anyone out there who may have the same problem when compiling, here is the same thing but in es5:
array = ["Alpha_GB8732", "Beta_GB29834", "Gamma_GB2384", "Delta_GB23984"]
var result = array.filter(function (el) {
return el.includes("Alpha_GB") === true;
});
console.log(result);
Again, thanks a lot for your help everyone! (@Louis, @ControlAltDel)