I am trying to find out if the given key exists in array of object. if the value key exists then i want to return true else false.
i giving input of key from text box and then checking if the key exists in array of objects, but i couldn't get it.
here is what i have tried
code:
var obj = [{
"7364234":"hsjd",
"tom and jerry":"dsjdas",
"mickey mouse":"kfjskdsad",
"popeye the sailor man":"alkdsajd",
"the carribean":"kasjdsjad"
}]
var val = $("input[name='type_ahead_input']").val();
if (obj[val]) {
console.log('exists');
} else {
console.log('does not exist');
}
if i give input as 'the carribean
' which exists in array of object, even then its outputting in console as does not exist.
how can i resolve this?
Using hasOwnProperty() function The function hasOwnProperty() will check for the existence of a key in the given object and returns true if the key is present or else it returns false. This function takes the key of the object as the parameter and returns the Boolean result accordingly.
There are mainly two methods to check the existence of a key in JavaScript Object. The first one is using “in operator” and the second one is using “hasOwnProperty() method”. Method 1: Using 'in' operator: The in operator returns a boolean value if the specified property is in the object.
The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.
The best way to check for a key's existence in TypeScript is to explicitly check if accessing the specific key in the object returns a value of undefined . Copied! The department property has a type of string or undefined because it's marked as optional. However, in the if statement, its type is string .
you can use typeof
to check if key
exist
if (typeof obj[0][val] !== "undefined" ) {
console.log('exists');
} else {
console.log('does not exist');
}
Note: There is index 0
coz the object that you are checking is an element 0 of the array obj
Here is a fiddle:
var obj = [{
"7364234":"hsjd",
"tom and jerry":"dsjdas",
"mickey mouse":"kfjskdsad",
"popeye the sailor man":"alkdsajd",
"the carribean":"kasjdsjad"
}];
if ( typeof obj[0]["the carribean"] !== 'undefined' ) {
console.log('exists');
} else {
console.log('does not exist');
}
As suggested by Cristy below, you can also use obj[0][val] === undefined
You can also:
var obj = [{
"7364234":"hsjd",
"tom and jerry":"dsjdas",
"mickey mouse":"kfjskdsad",
"popeye the sailor man":"alkdsajd",
"the carribean":"kasjdsjad"
}];
var val = "7364234";
if ( val in obj[0] ) {
console.log('exists');
} else {
console.log('does not exist');
}
I suggest using Array.some()
to know if a key exists and Array.find()
to get the value of the found key, together with some recent syntax:
let arr = [{"foo": 1}, {"bar":2}];
function isKeyInArray(array, key) {
return array.some(obj => obj.hasOwnProperty(key));
}
function getValueFromKeyInArray(array, key) {
return array.find(obj => obj[key])?.[key];
}
console.log(isKeyInArray(arr, "foo"), isKeyInArray(arr, "bar"), isKeyInArray(arr, "baz"));
console.log(getValueFromKeyInArray(arr, "foo"), getValueFromKeyInArray(arr, "bar"), getValueFromKeyInArray(arr, "baz"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With