"함수 containsKey()"의 두 판 사이의 차이

 
(사용자 2명의 중간 판 25개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[분류: Dictionary]]
[[분류: Dictionary]]
==개요==
;array_key_exists()
;array_key_exists()
;.hasOwnProperty()
;.containsKey()
;.containsKey()
* dictionary에 해당 key가 있는지 판별하는 함수/메소드
==C++==
{{참고|C++ 맵 키 있는지 확인}}
<syntaxhighlight lang='cpp' run>
#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
}
</syntaxhighlight>
==Go==
{{참고|Go Map containsKey()}}
<syntaxhighlight lang='go' run>
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
}
</syntaxhighlight>


==Java==
==Java==
{{참고|자바 HashMap .containsKey()}}
{{참고|자바 HashMap.containsKey()}}
[[분류: Java]]
[[분류: Java]]
<source lang='java'>
<syntaxhighlight lang='java'>
HashMap<String,Object> hm = new HashMap<String,Object>();
HashMap<String,Object> hm = new HashMap<String,Object>();
hm.put("first", "1");
hm.put("first", "1");
hm.put("second", "4");
hm.put("second", "4");
</source>
 
System.out.println( hm.containsKey("first") );
System.out.println( hm.containsKey("third") );
// true
// false
</syntaxhighlight>
 
==JavaScript==
{{참고|자바스크립트 hasOwnProperty()}}
[[분류: JavaScript]]
<syntaxhighlight lang='JavaScript' run>
let x = {first:1, second:4};
console.log( 'first' in x ); // true
console.log( 'thrid' in x ); // false
</syntaxhighlight>
<syntaxhighlight lang='JavaScript' run>
let x = {first:1, second:4};
console.log( x.hasOwnProperty('first' ) ); // true
console.log( x.hasOwnProperty('thrid' ) ); // false
</syntaxhighlight>


==PHP==
==PHP==
{{참고|PHP array_key_exists()}}
{{참고|PHP array_key_exists()}}
[[분류: PHP]]
[[분류: PHP]]
<source lang='php'>
<syntaxhighlight lang='php' run>
$arr = ['first'=>1, 'second'=>4];
$arr = ['first'=>1, 'second'=>4];
var_dump(array_key_exists('first', $arr));
var_dump(array_key_exists('first', $arr)); # bool(true)
var_dump(array_key_exists('third', $arr));
var_dump(array_key_exists('third', $arr)); # bool(false)
# bool(true)
</syntaxhighlight>
# bool(false)
 
</source>
==Python==
[[분류: Python]]
{{참고|파이썬 has_key()}}
<syntaxhighlight lang='Python' run>
dict = {'first':1, 'second':4}
print('first' in dict) # True
print('third' in dict) # False
</syntaxhighlight>


==같이 보기==
==같이 보기==
* [[함수 has_value()]]
* [[함수 keys()]]
* [[함수 is_defined()]]
* [[함수 is_defined()]]
* [[함수 index()]]

2023년 11월 23일 (목) 00:33 기준 최신판

1 개요[ | ]

array_key_exists()
.hasOwnProperty()
.containsKey()
  • dictionary에 해당 key가 있는지 판별하는 함수/메소드

2 C++[ | ]

#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
}

3 Go[ | ]

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
}

4 Java[ | ]

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[ | ]

let x = {first:1, second:4};
console.log( 'first' in x ); // true
console.log( 'thrid' in x ); // false
let x = {first:1, second:4};
console.log( x.hasOwnProperty('first' ) ); // true
console.log( x.hasOwnProperty('thrid' ) ); // false

6 PHP[ | ]

$arr = ['first'=>1, 'second'=>4];
var_dump(array_key_exists('first', $arr)); # bool(true)
var_dump(array_key_exists('third', $arr)); # bool(false)

7 Python[ | ]

dict = {'first':1, 'second':4}
print('first' in dict) # True
print('third' in dict) # False

8 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}