Get Keys (associative array)

From SwiftAPI

Jump to: navigation, search

Contents

C#

Dictionary<string, int> a = new Dictionary<string, int>() { { "test", 4 }, { "foo", 2 } };
Dictionary<string, int>.KeyCollection keys = a.Keys;

Java

import java.util.*;
 
Map<String, Integer> a = new HashMap<String, Integer>();
a.put("test", 4);
a.put("foo", 2);
Set<String> keys = a.keySet();

JavaScript

ECMAScript 5.1, Prototype

IE 9+, FireFox 4+, Chrome 5+, Safari 5+

Mozilla

var a = {'test': 4, 'foo': 2};
var keys = Object.keys(a);
// keys = ['test', 'foo']

Objective-C

developer.apple.com

@property(readonly, copy) NSArray *allKeys
NSDictionary *a = @{@"test": @4, @"foo": @2};
NSArray *keys = a.allKeys;
// keys == [@"test", @"foo"]

Perl

%a = ('test' => 4, 'foo' => 2);
@keys = keys(%a);
# @keys == ('test', 'foo')

PHP

$a = array('test' => 4, 'foo' => 2);
$keys = array_keys($a);
// $keys == array('test', 'foo')

Python

a = {'test': 4, 'foo': 2}
keys = a.keys()
# keys = ['test', 'foo']

Ruby

a = {"test" => 4, "foo" => 2}
keys = a.keys
# keys == ["foo", "test"]

Swift

let a = ["test": 4, "foo": 2]
let keys = Array(a.keys)
// keys == ["test", "foo"]
Personal tools