• Register
1 vote
37 views

Problem:

I just jump into the JavaScript learning process. I am grabbing the language day by day gradually. I want to declare an object or an array in my code and check whether the key is available or not in it.  So, let me put my question this way, in javascript check if key exists in array?

I’d really appreciate your effort and help. Thanks a lot, mate.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

1 vote
 
Best answer

Solution:

There are several ways to check if the keys exist in an object or array in JavaScript.

For objects, you can use the in operator to check the keys’ existence. This is a boolean type operator and throws back a True/False output

let myobj = {
   name: "Roy",
   age: 25
   id: 01
}
console.log('name' in myobj);
console.log('address' in myobj);
console.log('id' in myobj);

Output will be

true
false
true

For the case of an array, you can use the indexOf() method. This method returns -1 if the key doesn’t exist in the array else it returns the index of the array.

let myarr = ["Roy", 5, 2, "John", 23.5];
console.log(myarr.indexOf({}))
console.log(myarr.indexOf("Roy"))
console.log(myarr.indexOf(23.5))

And the output it throws is

-1
0
4

 

13 9 6
94,240 points

Related questions

0 votes
1 answer 177 views
177 views
I am new to this. I just want to know how to do that?
asked Nov 28, 2020 TeamScript 13.5k points
0 votes
1 answer 13 views
13 views
Problem: Is there anyone who can help me so i can run my code to check if a key exists in a javascript object please help me. How can I check if a specific key exists in a JavaScript object or array? If the key doesn't exist and I try to access it, will it return false? Or throw a mistake?
asked Nov 10, 2020 Han Li 710 points
0 votes
1 answer 27 views
27 views
I was working on that. I just want to know how to do that?
asked Nov 7, 2020 TeamScript 13.5k points
0 votes
1 answer 34 views
34 views
I am new to this. I just want to know how to do that ?
asked Nov 3, 2020 TeamScript 13.5k points
0 votes
1 answer 15 views
15 views
I was working on that but I got into some problems.
asked Nov 17, 2020 TeamScript 13.5k points
0 votes
1 answer 9 views
9 views
I was working on that but I got into some problems.
asked Nov 3, 2020 TeamScript 13.5k points
0 votes
1 answer 7 views
7 views
Problem: For loading the first page I need to check if there is an image in image_arrayand load the last image, otherwise lock the preview buttons; prompt the user to press the new image button; create an empty array to put the images there: The problem is that image_arraythe ... : ?> <script type="text/javascript"> <?php echo "image_array = ".json_encode($images);?> </script> <?php endif; ?>
asked Oct 21, 2020 sasha 8.7k points
0 votes
1 answer 12 views
12 views
Problem: I have a function that will check a serialized data form if there are duplicate values in it. s = $('#multiselectForm').serialize(); var x = []; var y = []; x = s.split(&); for (var i = x.length - 1; i >= 0; i--) { y.push(x[i].split(=)); }; var c ... Array [ 0: 'my_field2' 1: 'val2' ], Array [ 0: 'my_field3' 1: 'val1' ] ] Is there a better way than to do the same task? Maybe even shorter?
asked Oct 18, 2020 sasha 8.7k points
0 votes
1 answer 6 views
6 views
Problem: How can I check if a string contains another substring in javascript?Generally I would expect a method String.contains(), but there doesn't seem to be one.
asked Feb 12 sasha 8.7k points
0 votes
1 answer 2 views
2 views
Problem: I want to check if the key exists in the dictionary using C#. Kindly Help..!
asked 2 days ago Umar Ahmad 2.1k points