1 개요[ | ]
- array_key_exists()
- .hasOwnProperty()
- .containsKey()
- dictionary에 해당 key가 있는지 판별하는 함수/메소드
2 C++[ | ]

C++
Copy
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string,int> m = {{"apple",100}, {"banana",2}};
cout << (m.find("apple") != m.end()) << endl; // 1: found
cout << (m.find("cranberry") != m.end()) << endl; // 0: not found
}
Loading
3 Go[ | ]

Go
Copy
package main
import "fmt"
func main() {
var dict = map[string]int{
"apple": 40,
"banana": 70,
}
var exists bool
_, exists = dict["apple"]
fmt.Println(exists) // true
_, exists = dict["lemon"]
fmt.Println(exists) // false
}
Loading
4 Java[ | ]

Java
Copy
HashMap<String,Object> hm = new HashMap<String,Object>();
hm.put("first", "1");
hm.put("second", "4");
System.out.println( hm.containsKey("first") );
System.out.println( hm.containsKey("third") );
// true
// false
5 JavaScript[ | ]

JavaScript
Copy
let x = {first:1, second:4};
console.log( 'first' in x ); // true
console.log( 'thrid' in x ); // false
▶ | true |
▶ | false |
JavaScript
Copy
let x = {first:1, second:4};
console.log( x.hasOwnProperty('first' ) ); // true
console.log( x.hasOwnProperty('thrid' ) ); // false
▶ | true |
▶ | false |
6 PHP[ | ]

PHP
Copy
$arr = ['first'=>1, 'second'=>4];
var_dump(array_key_exists('first', $arr)); # bool(true)
var_dump(array_key_exists('third', $arr)); # bool(false)
Loading
7 Python[ | ]

Python
Copy
dict = {'first':1, 'second':4}
print('first' in dict) # True
print('third' in dict) # False
Loading
8 같이 보기[ | ]
로그인하시면 댓글을 쓸 수 있습니다.