Как определить, имеет ли ассоциативный массив ключ?

Я могу использовать import("mod") для доступа к немодульным скриптам.

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-9.html#import-types

Вот так:

declare module 'ui-library' {
  export const Icon: React.FC;
}

21
задан Soviut 29 March 2009 в 19:39
поделиться

4 ответа

var card:Object = {name:"Tom"};

trace("age" in card);  //  return false 
trace("name" in card);  //  return true

Попробуйте этот оператор: "в"

37
ответ дан 29 November 2019 в 20:06
поделиться

hasOwnPropery - это один из способов его проверки. Возьмем для примера:


var dict: Dictionary = new Dictionary();

// this will be false because "foo" doesn't exist
trace(dict.hasOwnProperty("foo"));

// add foo
dict["foo"] = "bar";

// now this will be true because "foo" does exist
trace(dict.hasOwnProperty("foo"));
5
ответ дан 29 November 2019 в 20:06
поделиться

Самый быстрый путь может быть самым простым:

// creates 2 instances
var obj1:Object = new Object();
var obj2:Object = new Object();

// creates the dictionary
var dict:Dictionary = new Dictionary();

// adding the first object to the dictionary (but not the second one)
dict[obj1] = "added";

// checks whether the keys exist
var test1:Boolean = (dict[obj1] != undefined); 
var test2:Boolean = (dict[obj2] != undefined); 

// outputs the result
trace(test1,test2);
4
ответ дан 29 November 2019 в 20:06
поделиться

Попробуйте это:

for (var key in myArray) {
    if (key == myKey) trace(myKey+' found. has value: '+myArray['key']);
}
1
ответ дан 29 November 2019 в 20:06
поделиться
Другие вопросы по тегам:

Похожие вопросы: