0

I have a value, and an array. I'm trying to see if the value is contained within the array. I'm using jQuery's 'inArray' method but the results keep telling me that it is not found. What am I doing wrong?

here is the fiddle. http://jsfiddle.net/p9xcb/

var array =  ["79C8A605-1384-4A51-8AC9-C53C1AE075D2", "E367CEEE-A344-4C63-B4B3-A1CC20D3DAF2", "1E3ED40E-935A-4895-960E-89FFD8CD81B9", "2E67F9E8-4770-497E-A938-0A145DA033AF", "35B33B09-6BD3-495E-9A3E-4B5D6DF8655E", "F419FB1F-4922-46B9-B651-8BB0D42510DD", "61FD263B-175E-4434-8A88-8E00418E2E14", null, "FF98229A-998C-49AD-9392-E4CE98115C0A", "C88B9D52-FE70-430C-B43D-D52F9B22E997", "1DDF2413-B453-42BB-8D78-7A7AAA94C353", "55D922B1-EBCD-4EC3-87A5-FA6AA38C9975", "0FA8079A-F76F-4CA5-B370-7B8B9AF2720A", "CF1971EF-E0C2-4C54-8131-A19FB14F2515", "A736854B-73A2-4282-85E0-6298D8AB815A", "DA280665-DC5E-4DF2-9FB1-924E1C8E4713", "9C73A934-9DDF-415C-A409-F2AC79493539", "FD950617-2C9C-4B6C-94F1-6C746699B636", "F184BBBE-C296-4F1D-9DD7-57F87D3B95A6", "676955FB-3F3D-4DC6-8F9D-A873A8440499", "5509E5C6-6650-4DF6-B7A9-49C8BC935B53", "7D88DCD6-431E-41EB-9A87-5C19C2D579FC", "509EA3D8-5B30-41F0-809B-D1E45BC30E3C", "6C4D6F4F-A188-4E6C-B40C-02AE4A5FFAAB", "710E2593-FE14-4CAB-B97C-95D96BB81266", "CBEB793E-A572-4995-8342-E9A48EF19E82", "A8C937F2-056C-46B4-8034-11CA4BB37A1D", "8E9B5E53-298B-4BDE-A696-20F102C28F12", "7BF7421C-CB21-4A44-BBA3-2CFD3A74C889", "F8D89224-7BB7-477F-BB8E-F5B2633A4111", "73F196CF-62F8-4F7A-9938-4F52404450E4", "B4D419D6-A5B8-4141-8C0E-ADCBEC72F2D8", "59ECF672-5C93-4E1E-8E30-6E0699874D61", "EC72A684-5888-48E1-99FD-FE455A50222F", "5D234D6D-E296-4D90-B87E-61FB6FB48702", "8EDD5718-E28A-4A4E-85BA-4DC9C072EAA8", "66FEA812-9945-4FFB-87CB-CCA828768FA0", "0025CB40-3016-4286-AD60-46685EDFD6A1", "508AC4D3-EA29-43A9-92CC-453FE2438D58", "030C5CBE-C227-4326-BED7-DB16A90D5A36", "2AAFF154-0FD0-4B60-9180-DE19A9E0890B", "64EBB0B4-81C1-46EB-8B1A-711A06E45FA3", "E1AA77B2-0E67-42A7-B7D7-A15D2A4EA448", "E9543440-E66E-4984-AC4E-091F161D33D6", "D93988D5-73D9-4988-B1A1-B48282F3C7B7", "6DC572B8-1F69-4A5C-81C7-6EAC617EBEE1", "BB56B36C-1D90-4B8D-ABF0-4ECD4052DCC1", "D1FEFB8D-84F4-4500-92F2-70FEE23F4B6E", "3A836ED4-639C-42C4-B945-375DC053FB61", "33672786-E964-4C3F-A3EB-7BF6634D13A0", "7F76C892-AEC1-4901-80BE-B93DFF59D159", "0E92F1AD-5C09-4B3D-BAF5-5D62BA3440EF", "73E50616-DCA7-4E83-B81F-100C21A18C49", "CF89704A-BD58-4D41-A5B5-10BAFC6F8085", "0D5CBF2A-3699-4968-A2DC-F6EA61D6C7C1", "FED50D50-86E8-42E2-8CD4-17DC01BB966C", "81EEFFA0-D172-4BC3-8474-2C55E6CB06CA", "8FA6CBBD-D5AC-431D-B1E3-C0D1BA48ECCA", "7EEA9381-F0F0-4252-BF29-D15A5A0283C6", "0FFDE30B-E4C3-40CC-A292-566D54D05821", "E24EE39E-0FDE-4C20-B61C-E5C07B483CF8", "82F6B86F-6EAB-42B4-B19D-CA6EAFF83A92", "7B663073-6296-4A08-89AF-7051EAFC3E8A", "EA36E8B5-A266-473C-A076-96A605A856B9", "4A99A5A0-FBF3-4AC6-A34A-52E761B98C8A", "26513617-D98B-44D9-BDD5-2FFCB8C7783B", "67F93D0F-E145-4447-A7B8-E3D1E944B88C", "9798FD1D-B781-4420-A37E-622E04E69672", "1A872624-E81B-45B7-B6FD-ED229AE54042", "092C0992-FAED-4E74-AB3E-6553F5261EF2", "76B94E6B-BE6B-43F2-ABBD-67A3B92C33B9", "AADF1B4D-F32E-4C0C-B4FC-EAC051338B8C", "E8A2479D-B1FC-4B67-AC94-C1AB8E1C7298", "8F7350C0-867D-461A-92D9-225CF8E91D60", "E742D55B-ADF0-4ADF-A3C9-EC4B3BAA832A", "320EBB4F-F0AB-47E7-A5B6-334429F8DEC8", "CEF5D66B-5EF2-468B-A9BE-06B0BC05E917", "CC6D2205-EB58-414D-A404-ED7B743E385E", "7D7EB4D0-BE31-4F28-928D-8E1FC5C947F0"];

var value = "CC6D2205-EB58-414D-A404-ED7B743E385E";

$('body').html($.inArray(value, array));​

Edit:

If I run this code

if ($.inArray(value, array)) {/*this will never be called, why?*/} 

I will never see anything where /*this will never be called, why?*/ section is.

6
  • It returns 77 in that fiddle. Commented Nov 1, 2012 at 16:20
  • Yeah I edited my question to reflect it not saying -1 any more Commented Nov 1, 2012 at 16:21
  • @DennisMartinez - What do you mean by "not found" then? It is found, at index 77. Commented Nov 1, 2012 at 16:21
  • if i call $.inArray() as like a check such as if ($.inArray(v, a)) {/*this will never be called, why?*/} Commented Nov 1, 2012 at 16:22
  • I get a 500 internal server error :) Commented Nov 1, 2012 at 16:22

4 Answers 4

1

You should first understand the concept of truthyness/falseyness in javascript. The following values will evaluate inside a boolean operator as false

undefined
null
empty string
false
0

And the following will evaluate true

non-null object reference
non-empty string
true
any non-zero number

Now do you see the problem using

if($.inArray(value,array)){

}

Hint: Not finding the element (-1) will evaluate true, finding the element in the first position (0) will evaluate false. Finding the value in any element after the 1st will evaluate true.

The easy fix is to use:

if ($.inArray(value, array) >= 0)
Sign up to request clarification or add additional context in comments.

1 Comment

Not finding the element (-1) will evaluate false - this is not true. if(-1){true} returns true. -1 == true returns false. See this question: why-are-javascript-negative-numbers-not-always-true-or-false
1

The $.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match. If the first element within the array matches value, $.inArray() returns 0.

In addition to that inArray returns the index of the occurrence.

Comments

1

This might be your problem. In javascript if (0) is going to be the same as if (false), and if (-1) is the same as if (true). You need to use

if ($.inArray(value, array) >= 0)

Comments

0
check = function (arr,val)
{
    for(i = 0; i < arr.length;i++){
        try{
         if(arr[i].indexOf(val) != -1){
            return i;           
        }
        }catch(e){}
    }
    return -1;
}

use it like this check(array,value);

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.