2

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)

4

3 Answers 3

1

Based on ControlAltDel's comment, you can use Array.filter(). See the example below;

let array = ["Alpha_GB8732", "Beta_GB29834", "Gamma_GB2384", "Delta_GB23984"]
let result = array.filter((el) => el.includes("Alpha_GB") === true);
console.log(result);

// You could turn this in to a function
function arrayContains(arr, str){
  return arr.filter((el) => el.includes("Alpha_GB") === true).length > 0;
}

console.log(arrayContains(array, 'Alpha_GB')); // returns true

// And use it like this
//if(arrayContains(array, 'Alpha_GB')){}...

Sign up to request clarification or add additional context in comments.

1 Comment

This is a great solution as I really needed it to only include the string as opposed to being an exact match. Thanks!
1

You could use filter, like

array = ["Alpha_GB8732", "Beta_GB29834", "Gamma_GB2384", "Delta_GB23984"]
var find = array.filter(function(item) {
  return (this === "Alpha_GB8732");
});
find = (find.length > 0) ? find[0] : null;

3 Comments

Ah, I didn't know you'd post an answer too =) Mine's based on your comment. FWIW, this is a better implementation anyway.
Actually, won't yours only return true if there's an exact full-string match?
@Lewis yes. But nothing is stopping you from using a regex in place of string equals.
1

So you need to filter out the array based upon some characters inside it This is how it should work:

const MyArray = ["Alpha_GB8732", "Beta_GB29834", "Gamma_GB2384", "Delta_GB23984"]

//you can replace .startswith() with any function that you might need. you can also apply .toLowerCase() to filter case-insensitive items

const filteredArray = MyArray.filter((x) => x.startsWith("N"));

console.log(filteredArray);

Hope it helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.