我如何确定一个对象x是否具有定义的属性y,而不管x.y的值是多少?
我正在使用
if (typeof(x.y) !== 'undefined')
但这似乎有点笨拙。有没有更好的办法?
我如何确定一个对象x是否具有定义的属性y,而不管x.y的值是多少?
我正在使用
if (typeof(x.y) !== 'undefined')
但这似乎有点笨拙。有没有更好的办法?
当前回答
如果你想知道对象物理上是否包含属性@gnarf的答案使用hasOwnProperty将完成工作。
如果你想知道属性是否存在,无论是在对象本身上还是在原型链上,你可以使用in操作符。
if ('prop' in obj) {
// ...
}
Eg.:
var obj = {};
'toString' in obj == true; // inherited from Object.prototype
obj.hasOwnProperty('toString') == false; // doesn't contains it physically
其他回答
与此线程中的其他示例不同,此实现仅断言对象具有我们正在检查的属性。
const hasOwnProperty = <X extends {}, Y extends PropertyKey>(
object: X,
property: Y
): object is Record<Y, unknown> & X => {
return object.hasOwnProperty(property);
};
下面是一个使用它来标识具有所需属性的分支的示例。
const hasOwnProperty = <X extends {}, Y extends PropertyKey>(
object: X,
property: Y
): object is Record<Y, unknown> & X => {
return object.hasOwnProperty(property);
};
type PaidProjectFeeType = 'FIXED_PRICE' | 'RATE' | '%future added value';
const PAID_PROJECT_FEE_TYPE_LABELS: Record<
'FIXED_PRICE' | 'RATE',
string
> = {
FIXED_PRICE: 'Fixed Price',
RATE: 'Rate',
};
export const getPaidProjectFeeTypeLabel = (
feeType: PaidProjectFeeType
): string => {
if (hasOwnProperty(PAID_PROJECT_FEE_TYPE_LABELS, feeType)) {
PAID_PROJECT_FEE_TYPE_LABELS[feeType];
}
throw new Error('test');
};
https://tsplay.dev/m0LBOm
令人恼火的是,现在PAID_PROJECT_FEE_TYPE_LABELS被假设为:
Record<PaidProjectFeeType, unknown> & Record<"FIXED_PRICE" | "RATE", string>
也就是说,你不能返回结果,因为X[Y]的可能值是未知的。当您需要断言对象具有所需的属性时,这很有用,但您将需要添加进一步的断言以确保结果是您想要的。
不过,有一个更好的办法。
我们需要两个工具:
export const keys = <T extends Record<string, unknown>>(
object: T
): Array<keyof T> => {
return Object.keys(object);
};
Keys为我们提供了对象属性名的类型化数组。
export const includes = <C extends M, M>(
collection: readonly C[],
member: M
): member is C => {
return collection.includes(member as C);
};
Includes允许断言某个属性是只读数组的成员。你可以在这篇博客文章中阅读更多关于includes的内容。
export const keys = <T extends Record<string, unknown>>(
object: T
): Array<keyof T> => {
return Object.keys(object);
};
export const includes = <C extends M, M>(
collection: readonly C[],
member: M
): member is C => {
return collection.includes(member as C);
};
type PaidProjectFeeType = 'FIXED_PRICE' | 'RATE' | '%future added value';
const PAID_PROJECT_FEE_TYPE_LABELS: Record<
'FIXED_PRICE' | 'RATE',
string
> = {
FIXED_PRICE: 'Fixed Price',
RATE: 'Rate',
};
export const getPaidProjectFeeTypeLabel = (
feeType: PaidProjectFeeType
): string => {
if (includes(keys(PAID_PROJECT_FEE_TYPE_LABELS), feeType)) {
return PAID_PROJECT_FEE_TYPE_LABELS[feeType];
}
throw new Error('test');
};
https://tsplay.dev/N7gLDN
简而言之,这种方法允许我们将feeType值缩小到键中存在的值(PAID_PROJECT_FEE_TYPE_LABELS),然后允许我们访问属性值。
这种方法效果最好,但需要注意的是,从技术上讲,键的实现不是运行时安全的。在运行时返回的值与使用tsc推断的值不同的情况下(主要是理论性的)。
包括
Object.keys(x).includes('y');
array .prototype.includes()方法确定数组在其条目中是否包含某个值,并根据需要返回true或false。
and
object. keys()返回一个字符串数组,表示给定对象的所有可枚举属性。
. hasownproperty()和ES6+ ?-option -chaining如:if (x?.y)是非常好的2020+选项。
既然问题是关于属性检查的笨拙,一个常规的用例是函数参数选项对象的验证,我认为我应该提到一种无库的测试多个属性是否存在的简短方法。 免责声明:它确实需要ECMAScript 5(但在我看来,任何仍在使用IE8的人都应该拥有一个破碎的网络)。
function f(opts) {
if(!["req1","req2"].every(opts.hasOwnProperty, opts)) {
throw new Error("IllegalArgumentException");
}
alert("ok");
}
f({req1: 123}); // error
f({req1: 123, req2: 456}); // ok
你可以像这样修剪一下:
if ( x.y !== undefined ) ...
为什么不简单地:
if (typeof myObject.myProperty == "undefined") alert("myProperty is not defined!");
或者如果你想要一个特定的类型:
if (typeof myObject.myProperty != "string") alert("myProperty has wrong type or does not exist!");