admin管理员组文章数量:1399306
I have an array of objects in Javascript that look like this:
[{width:100,height:50,name:"John"},{width:27,height:12,name:"John"},..]
If that Array size is bigger than 10, I would like to remove those objects which area (width*height) are smaller. So, if there are 20 objects, remove the 10 which area are smaller among the rest.
How could I do this?
Right now what I do is have a threshold in order to filter the objects. So I do this:
var i = elements.length;
var threshold = 100;
while (i--) {
if (elements[i].width * elements[i].height < threshold) {
elements.splice(i,1);
}
}
But this is not what I want. I don't want a static threshold, I just want to remove those which area is smaller than the rest top 10.
I have an array of objects in Javascript that look like this:
[{width:100,height:50,name:"John"},{width:27,height:12,name:"John"},..]
If that Array size is bigger than 10, I would like to remove those objects which area (width*height) are smaller. So, if there are 20 objects, remove the 10 which area are smaller among the rest.
How could I do this?
Right now what I do is have a threshold in order to filter the objects. So I do this:
var i = elements.length;
var threshold = 100;
while (i--) {
if (elements[i].width * elements[i].height < threshold) {
elements.splice(i,1);
}
}
But this is not what I want. I don't want a static threshold, I just want to remove those which area is smaller than the rest top 10.
Share Improve this question edited Jan 21, 2014 at 19:03 Hommer Smith asked Jan 21, 2014 at 18:58 Hommer SmithHommer Smith 27.9k62 gold badges176 silver badges307 bronze badges 1- 1 @Zzyrk edited with what I have. – Hommer Smith Commented Jan 21, 2014 at 19:04
3 Answers
Reset to default 10You can sort your array first by area and then use slice
to leave only 10 bigger. Something like this:
var arr = arr.sort(function(a, b) {
var as = a.width * a.height,
bs = b.width * b.height;
if (as > bs) return -1;
if (as < bs) return 1;
return 0;
})
.slice(0, 10);
Demo: http://jsfiddle/y765Z/
You could sort the array and then set its length to 10.
var areas = [{width:100,height:50,name:"John"},{width:27,height:12,name:"John"},...];
areas.sort(function(a, b){
return b.height * b.width - a.height * a.width;
});
areas.length = 10;
Note that this only works if you don't care if the array elements get reordered.
http://jsfiddle/g9Hk7/
One other possibility, if you need to keep them in order is this somewhat strange little bit here:
arr.map(function(item, index) {
return {index: index, area: item.height * item.width};
}).sort(function(a, b) {
return b.area - a.area;
}).slice(10).sort(function(a, b) {
return b.index - a.index;
}).reduce(function(arr, item) {
arr.splice(item.index, 1);
return arr;
}, arr);
This modifies the original array, but you can change that by replacing the last line with
}, clone(arr));
using some appropriate clone function, perhaps this:
var clone = function(obj) {return JSON.parse(JSON.stringify(obj));};
Obviously if you wanted to turn this into a function, you could generalize the 10
, and it would pretty easy to abstract from area
into an arbitrary function on your objects as well.
本文标签: javascriptRemove elements from an array until there are just 10 elementsStack Overflow
版权声明:本文标题:javascript - Remove elements from an array until there are just 10 elements - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744204580a2595125.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论