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

22번째 줄: 22번째 줄:


_, exists = dict["apple"]
_, exists = dict["apple"]
fmt.Println(ok) // true
fmt.Println(exists) // true


_, exists = dict["lemon"]
_, exists = dict["lemon"]
fmt.Println(ok) // false
fmt.Println(exists) // false
}
}
</syntaxhighlight>
</syntaxhighlight>

2022년 2월 15일 (화) 11:59 판

1 개요

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

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

3 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

4 JavaScript

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

5 PHP

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

6 Python

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

7 같이 보기

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