[英]Sort array by Object Property on javascript
I have this code and i need to sort each element of the array by area .我有这段代码,我需要按区域对数组的每个元素进行排序。
function Rectangle(base, altura) {
this.base = base;
this.altura = altura;
this.area = function () {
return this.base * this.altura;
};
this.perimetre = function () {
return 2 * (this.base + this.altura);
};
this.toString = function () {
return (
'(b= ' +
this.base +
', h= ' +
this.altura +
', a = ' +
this.area() +
', p =' +
this.perimetre() +
')'
);
};
}
var rectangles = [
new Rectangle(1, 1),
new Rectangle(2, 2.05),
new Rectangle(2, 5),
new Rectangle(1, 3),
new Rectangle(4, 4),
new Rectangle(2, 8)
];
I need to do it by first declaring the function in the Array class of javascript and then sort it by using the sort() method.我需要首先在javascript的数组 class 中声明 function ,然后使用sort()方法对其进行排序。
array.prototype.ordenaPerArea = function() {
};
How can I do this?我怎样才能做到这一点?
You can use just subtract the areas for comparing.您可以只使用减去比较区域。
function Rectangle(base, altura) { this.base = base; this.altura = altura; this.area = function() { return (this.base * this.altura); } this.perimetre = function() { return 2 * (this.base + this.altura); } this.toString = function() { return "(b= " + this.base + ", h= " + this.altura + ", a = " + this.area() + ", p =" + this.perimetre() + ")"; } } var rectangles = [ new Rectangle(1, 1), new Rectangle(2, 2.05), new Rectangle(2, 5), new Rectangle(1, 3), new Rectangle(4, 4), new Rectangle(2, 8) ]; rectangles.sort((a, b) => a.area() - b.area()); rectangles.forEach(x => console.log(x.toString()));
If you really need to modify Array's prototype, you could, but it is not recommended.如果你真的需要修改 Array 的原型,你可以,但不推荐。
function Rectangle(base, altura) { this.base = base; this.altura = altura; this.area = function() { return (this.base * this.altura); } this.perimetre = function() { return 2 * (this.base + this.altura); } this.toString = function() { return "(b= " + this.base + ", h= " + this.altura + ", a = " + this.area() + ", p =" + this.perimetre() + ")"; } } var rectangles = [ new Rectangle(1, 1), new Rectangle(2, 2.05), new Rectangle(2, 5), new Rectangle(1, 3), new Rectangle(4, 4), new Rectangle(2, 8) ]; Array.prototype.ordenaPerArea = function(){ return this.sort((a, b) => a.area() - b.area()); } rectangles.ordenaPerArea(); rectangles.forEach(x => console.log(x.toString()));
I think what you are looking for is:我认为您正在寻找的是:
function Rectangle(base, altura) { this.base = base; this.altura = altura; this.area = function() { return this.base * this.altura; }; this.perimetre = function() { return 2 * (this.base + this.altura); }; this.toString = function() { return ( '(b= ' + this.base + ', h= ' + this.altura + ', a = ' + this.area() + ', p =' + this.perimetre() + ')' ); }; } var rectangles = [ new Rectangle(1, 1), new Rectangle(2, 2.05), new Rectangle(2, 5), new Rectangle(1, 3), new Rectangle(4, 4), new Rectangle(2, 8) ]; console.log('rectangles (before):'); rectangles.forEach(item => console.log(item.area())); Array.prototype.ordenaPerArea = function() { return this.sort(function(rectA, rectB) { return rectA.area() - rectB.area(); }); } rectangles.ordenaPerArea(); console.log('rectangles (after):'); rectangles.forEach(item => console.log(item.area()));
function Rectangle(base, altura) { this.base = base; this.altura = altura; this.area = function() { return this.base * this.altura; }; this.perimetre = function() { return 2 * (this.base + this.altura); }; this.toString = function() { return ( '(b= ' + this.base + ', h= ' + this.altura + ', a = ' + this.area() + ', p =' + this.perimetre() + ')' ); }; } var rectangles = [ new Rectangle(1, 1), new Rectangle(2, 2.05), new Rectangle(2, 5), new Rectangle(1, 3), new Rectangle(4, 4), new Rectangle(2, 8) ]; console.log('rectangles (before):'); rectangles.forEach(item => console.log(item.area())); Array.prototype.ordenaPerArea = function() { return this.map(item => item).sort(function(rectA, rectB) { return rectA.area() - rectB.area(); }); } const orderedRectangles = rectangles.ordenaPerArea(); console.log('orderedRectangles:'); orderedRectangles.forEach(item => console.log(item.area())); console.log('rectangles (after):'); rectangles.forEach(item => console.log(item.area()));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.