Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anand_aks's avatar

Checking if a value is present using Jquery.inArray()

I am retrieving the session array from the controller using ajax, then in the loop checking the required value is present in the array index.

barcode = '010001'
data['session'] = {
    "010001": {
        "id": "010001",
        "qty": 2
    },
    "010002": {
        "id": "010002",
        "qty": 1
    }
}

// checking if the barcode is present in the array index
if(jQuery.inArray(barcode,data['session']) !== -1)
{
	// barcode exist in the array
}


But I am always getting -1 as result

0 likes
2 replies
ftiersch's avatar
  1. You're checking an object, not an array
  2. inArray checks values, you are trying to check keys with it :)
gitwithravish's avatar

@anand_aks Hey anand. You need to do it like this

if(jQuery.inArray(barcode, Object.keys(data['session'])) !== -1)
{
	// barcode exist in the array
}

1 like

Please or to participate in this conversation.