Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if key exists in array of object

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?

like image 414
CJAY Avatar asked Jan 20 '18 13:01

CJAY


People also ask

How do I check if a key is present in an array of objects?

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.

How do you check if a key exist in an object?

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.

How do you find the key of an array?

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.

How do you check if a key exists in an object TypeScript?

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 .


2 Answers

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');
}
like image 164
Eddie Avatar answered Oct 20 '22 14:10

Eddie


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"));
like image 34
RiZKiT Avatar answered Oct 20 '22 16:10

RiZKiT