Javascript 按字符串对对象数组进行排序,与按不同行为的数值排序
这个问题有点啰嗦,但请耐心听我说 我有一个通用的值比较函数用于。排序: 内部帮助器函数GetGenderantProperty用于通过点路径安全地访问对象上的值:Javascript 按字符串对对象数组进行排序,与按不同行为的数值排序,javascript,typescript,Javascript,Typescript,这个问题有点啰嗦,但请耐心听我说 我有一个通用的值比较函数用于。排序: 内部帮助器函数GetGenderantProperty用于通过点路径安全地访问对象上的值: export function getDescendantProperty(obj: object, path: string) { // safely access nested properties if the property of the object is itself, an object if (path.i
export function getDescendantProperty(obj: object, path: string) {
// safely access nested properties if the property of the object is itself, an object
if (path.includes('.')) {
return path.split('.').reduce((accumulator: object, part: string) => accumulator && accumulator[part] || undefined, obj);
}
else {
return obj[path];
}
}
当我使用要排序的字符串对对象数组进行排序时,compareObjectValues按预期工作。当使用数值时,它会失败,并且不会遍历所有要比较的预期值
例如:
describe('on an object that uses a number for sorting', () => {
it('should return the comparison of the values in the correct order', () => {
const objects = [
{ id: 4 },
{ id: 0 },
{ id: 2 },
{ id: 3 },
{ id: 1 },
];
const sortedObjects = objects.sort(utils.compareObjectValues('id'));
expect(sortedObjects).toEqual([{ id: 0 }, { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }]);
});
});
业力输出:
LOG: 'comparison', 1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 0 of 2 SUCCESS (0 secs / 0 secs)
LOG: 'comparison', -1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 0 of 2 SUCCESS (0 secs / 0 secs)
LOG: 'comparison', -1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 0 of 2 SUCCESS (0 secs / 0 secs)
Chrome 74.0.3729 (Mac OS X 10.14.5) Utils tests When calling `compareObjectValues` on an object that uses a number for sorting should return the comparison of the numbers in the correct order FAILED
Error: Expected $[0].id = 4 to equal 0.
Expected $[1].id = 0 to equal 1.
Expected $[2].id = 1 to equal 2.
Expected $[3].id = 2 to equal 3.
Expected $[4].id = 3 to equal 4.
at <Jasmine>
at UserContext.<anonymous> (src/app/lib/utils.spec.ts:133:31)
at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:391:1)
at ProxyZoneSpec.push../node_modules/zone.js/dist/zone-testing.js.ProxyZoneSpec.onInvoke (node_modules/zone.js/dist/zone-testing.js:308:1)
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
Chrome 74.0.3729 (Mac OS X 10.14.5) Utils tests When calling `compareObjectValues` on an object that uses a number for sorting should return the comparison of the numbers in the correct order FAILED
Error: Expected $[0].id = 4 to equal 0.
Expected $[1].id = 0 to equal 1.
Expected $[2].id = 1 to equal 2.
Expected $[3].id = 2 to equal 3.
Expected $[4].id = 3 to equal 4.
at <Jasmine>
at UserContext.<anonymous> (src/app/lib/utils.spec.ts:133:31)
at ZoneDelegate../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (node_modules/zone.js/dist/zone.js:391:1)
LOG: 'string comparison', -1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
LOG: 'string comparison', 1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
LOG: 'string comparison', -1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
LOG: 'string comparison', 1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
LOG: 'string comparison', 1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
LOG: 'string comparison', -1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
LOG: 'string comparison', -1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
LOG: 'string comparison', -1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
LOG: 'string comparison', 1
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 1 of 2 (1 FAILED) (0 secs / 0.159 secs)
Chrome 74.0.3729 (Mac OS X 10.14.5): Executed 2 of 2 (1 FAILED) (0.19 secs / 0.16 secs)
TOTAL: 1 FAILED, 1 SUCCESS
TOTAL: 1 FAILED, 1 SUCCESS
npm ERR! Test failed. See above for more details.
从日志中可以看到,当处理带有数值的对象时,它在3次迭代后停止。使用字符串值,它处理9
我已验证GetDegenantProperty是否返回预期值。我还验证了,如果我在测试中将函数传递给.sort,手动应用相同的比较,则compareObjectValues的逻辑会按预期工作
我在隔离错误时遇到了一些困难,非常感谢您的帮助。问题在于对属性的延迟检查:
if (!propertyA || !propertyB) return 0;
这在检查某些类型的值时效果很好,但在检查数字时会产生意想不到的结果!0==真,空字符串!==真的等等
改用严格的检查:
if (typeof propertyA === 'undefined' || typeof propertyB === 'undefined') return 0;
如果房地产!propertyB{return 0;}让我给它一个tryI应该捕捉到的值。非常感谢你!
if (typeof propertyA === 'undefined' || typeof propertyB === 'undefined') return 0;