4
var name_list = ['Annie','John','Lyam','Mary']

var text_be_checked_1 = 'Annie'
do_some_stuff(name_list, text_be_checked_1) // true

var text_be_checked_2 = 'Ann'
do_some_stuff(name_list, text_be_checked_2) // false

var text_be_checked_3 = '1Annie'
do_some_stuff(name_list, text_be_checked_3) // true

var text_be_checked_4 = 'An2nie'
do_some_stuff(name_list, text_be_checked_4) // false

var text_be_checked_5 = 'Anni_John'
do_some_stuff(name_list, text_be_checked_5) // true

What I want is that determining whether text has full match with name in name_list like above.

I read this, but this is not exactly what I need.

How can I do this?

I don't care about where the solution came from. javascript or jQuery are all okay.

Would you solve this please?

EDIT:

Thank you everyone, There are many answers and I tested them.

But I thought that I have to add more explanation of my question.

You have to check this:

var name_list = ['Annie','John','Lyam','Mary']

var text_be_checked_3 = '1Annie'
do_some_stuff(name_list, text_be_checked_3) // true

As your answers, from here Mango can return true, but 1Mango, Mango_to returns false.

This is point, What I want is that 1Mango and Mango_to are also return true.

If this explanation is not enough, please comment me.

touchingtwist
  • 1,930
  • 4
  • 23
  • 38
  • 1
    it seems the same to me. Can you explain why its different? – kkica Jun 01 '18 at 10:50
  • 1
    I think you were misunderstood. Try this function inArray(needle, haystack) { var length = haystack.length; for(var i = 0; i < length; i++) { return needle.indexOf(haystack[i])!==-1 ; } return false; } var name_list = ['Annie','John','Lyam','Mary']; var output=inArray('1Annie', name_list); console.log(output); – kkica Jun 01 '18 at 11:04
  • 1
    you cannot im afraid. But glad i could help. @barmar – kkica Jun 01 '18 at 12:22

6 Answers6

2
function do_some_stuff(list, str) {
    return list.indexOf(str) !== -1
}

It is really that simple.

Array.indexOf() returns -1 if the item you are checking is not in the array. So by comparing the returned value to -1, you get a true if the item is in the list, and a false if it is not in the list.

stelioslogothetis
  • 9,371
  • 3
  • 28
  • 53
2

You can achieve it by simply doing: if (name_list.includes(text_to_be_checked) ) {}

Take your time to check out https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

Hope it helps.

Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63
Webvoid
  • 501
  • 2
  • 7
1

A functional approach:

name_list.filter(word => word == text_be_checked_1).length >= 1

Jonathan R
  • 3,652
  • 3
  • 22
  • 40
  • yeah, or `name_list.includes(text_be_checked_1)`, but why make things simple :) – Jeremy Thille Jun 01 '18 at 11:08
  • because this solution has already been mentioned.. why write it twice? – Jonathan R Jun 01 '18 at 11:10
  • Your `.filter` is not only more complicated to read, it's also way less efficient. It will filter the _whole_ array, even if the element is found at position 2 and the array contains 1000 elements. `.includes` will stop at 2. – Jeremy Thille Jun 01 '18 at 11:12
  • Did he ask for the most efficient solution or for A solution? I never said my solution is better than any but it works and can be applied to almost any language that supports functional programming – Jonathan R Jun 01 '18 at 11:13
1

name_list.some(name => name=== 'Mike') would return false
name_list.some(name => name=== 'Annie') would return true in your case.

The array method some returns true or false based on a condition. You can use it.

CodeF0x
  • 2,624
  • 6
  • 17
  • 28
Jhm
  • 101
  • 3
0

This is what you're actually looking for i guess,

var name_list = ['Annie','John','Lyam','Mary']

var text_be_checked_1 = 'Annie'

if($.inArray(text_be_checked_1, name_list) !== -1){
  console.log('value exist');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Hope this will helpful to you!

Greetings!

Geee
  • 2,217
  • 15
  • 30
0

Check Below Code as per your need it works :

function inArray(needle, haystack) {
    var length = haystack.length;
    for(var i = 0; i < length; i++) {
        if(haystack[i] == needle) return true;
    }
    return false;
}

var name_list = ['Annie','John','Lyam','Mary'];

var output=inArray('Annie', name_list);
console.log(output);
Dinesh Ghule
  • 3,423
  • 4
  • 19
  • 39