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

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(같은 사용자의 중간 판 11개는 보이지 않습니다)
1번째 줄: 1번째 줄:
[[분류: Dictionary]]
[[분류: Dictionary]]
{{DISPLAYTITLE:함수 has_key()}}
==개요==
==개요==
;array_key_exists()
;array_key_exists()
6번째 줄: 5번째 줄:
;.containsKey()
;.containsKey()
* dictionary에 해당 key가 있는지 판별하는 함수/메소드
* 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==
24번째 줄: 60번째 줄:
{{참고|자바스크립트 hasOwnProperty()}}  
{{참고|자바스크립트 hasOwnProperty()}}  
[[분류: JavaScript]]
[[분류: JavaScript]]
<syntaxhighlight lang='JavaScript'>
<syntaxhighlight lang='JavaScript' run>
var arr = {first:1, second:4};
let x = {first:1, second:4};
console.log( arr.hasOwnProperty('first' ) );
console.log( 'first' in x ); // true
// true
console.log( 'thrid' in x ); // false
console.log( arr.hasOwnProperty('thrid' ) );
</syntaxhighlight>
// false
<syntaxhighlight lang='JavaScript' run>
let x = {first:1, second:4};
console.log( x.hasOwnProperty('first' ) ); // true
console.log( x.hasOwnProperty('thrid' ) ); // false
</syntaxhighlight>
</syntaxhighlight>


35번째 줄: 74번째 줄:
{{참고|PHP array_key_exists()}}
{{참고|PHP array_key_exists()}}
[[분류: PHP]]
[[분류: PHP]]
<syntaxhighlight 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)
# bool(false)
</syntaxhighlight>
</syntaxhighlight>


46번째 줄: 83번째 줄:
[[분류: Python]]
[[분류: Python]]
{{참고|파이썬 has_key()}}
{{참고|파이썬 has_key()}}
<syntaxhighlight lang='Python'>
<syntaxhighlight lang='Python' run>
dict = {'first':1, 'second':4}
dict = {'first':1, 'second':4}
print('first' in dict)
print('first' in dict) # True
# True
print('third' in dict) # False
print('third' in dict)
# False
</syntaxhighlight>
</syntaxhighlight>



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