我正在编写一个函数来检查对象是否包含“id”或“serif:id”等键。不幸的是这不能正常工作。
function returnIdPreferSerifId(object) {
if (object.hasOwnProperty('serif:id' === true)) {
return object['serif:id'];
} else if (object.hasOwnProperty('id' === true)) {
return object.id;
} else {
console.log(object)
console.log(object.hasOwnProperty('serif:id' === true))
console.log(object.hasOwnProperty('id' === true))
throw `ID not found in Layer 1!`;
}
}
Run Code Online (Sandbox Code Playgroud)
测试对象为:
{
id: 'ska',
d: 'M255.848,145.321l19.839,0.054l12.677,8.62l6.085,-8.62l-8.62,-29.41l-30.488,13.637l0.507,15.719Z',
style: 'fill:rgb(187,222,251);stroke:white;stroke-width:1.33px;'
}
Run Code Online (Sandbox Code Playgroud)
我希望该函数返回“ska”,因为它是对象 ID。相反,控制台显示
{
id: 'ska',
d: 'M255.848,145.321l19.839,0.054l12.677,8.62l6.085,-8.62l-8.62,-29.41l-30.488,13.637l0.507,15.719Z',
style: 'fill:rgb(187,222,251);stroke:white;stroke-width:1.33px;'
}
Run Code Online (Sandbox Code Playgroud)
提前感谢您的帮助!