함수 containsKey()

(함수 array key exists()에서 넘어옴)

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