admin管理员组文章数量:1289703
Using jQuery, how can I iterate over an object, and get the unique values of a key with a count of each value?
For example, for this array:
var electrons = [
{ name: 'Electron1', distance: 1 },
{ name: 'Electron2', distance: 1 },
{ name: 'Electron3', distance: 2 },
{ name: 'Electron4', distance: 2 },
{ name: 'Electron5', distance: 2 },
{ name: 'Electron6', distance: 2 },
{ name: 'Electron7', distance: 2 },
{ name: 'Electron8', distance: 2 },
{ name: 'Electron9', distance: 2 },
{ name: 'Electron10', distance: 2 },
{ name: 'Electron11', distance: 3 },
];
I'd like to get back the following:
var distance_counts = {1: 2, 2: 8, 3: 1};
I've got this, which works but is a bit clumsy:
var radius_counts = {};
for (var i = 0; i < electrons.length; i++) {
if (electrons[i].distance in radius_counts) {
radius_counts[electrons[i].distance] += 1;
} else {
radius_counts[electrons[i].distance] = 1;
}
}
Using jQuery, how can I iterate over an object, and get the unique values of a key with a count of each value?
For example, for this array:
var electrons = [
{ name: 'Electron1', distance: 1 },
{ name: 'Electron2', distance: 1 },
{ name: 'Electron3', distance: 2 },
{ name: 'Electron4', distance: 2 },
{ name: 'Electron5', distance: 2 },
{ name: 'Electron6', distance: 2 },
{ name: 'Electron7', distance: 2 },
{ name: 'Electron8', distance: 2 },
{ name: 'Electron9', distance: 2 },
{ name: 'Electron10', distance: 2 },
{ name: 'Electron11', distance: 3 },
];
I'd like to get back the following:
var distance_counts = {1: 2, 2: 8, 3: 1};
I've got this, which works but is a bit clumsy:
var radius_counts = {};
for (var i = 0; i < electrons.length; i++) {
if (electrons[i].distance in radius_counts) {
radius_counts[electrons[i].distance] += 1;
} else {
radius_counts[electrons[i].distance] = 1;
}
}
Share
Improve this question
edited Sep 7, 2012 at 13:26
Richard
asked Sep 7, 2012 at 13:09
RichardRichard
65.6k135 gold badges356 silver badges570 bronze badges
4
- Try this stackoverflow./questions/1960473/unique-values-in-an-array – Artem Vyshniakov Commented Sep 7, 2012 at 13:10
- 2 @Artem - That's a pletely different question about getting an array of unique values. – Richard Commented Sep 7, 2012 at 13:25
- Will the initial array always be sorted as is? – sp00m Commented Sep 7, 2012 at 13:39
-
Would it be ok to use underscore.js? You could use
_.pluck
- although this will probably save you only one or two lines of code (and add the library....), a little like: jsfiddle/mYxVs – m90 Commented Sep 7, 2012 at 13:52
1 Answer
Reset to default 8you could use map for this purpose as:
var distances = {};
$.map(electrons,function(e,i) {
distances[e.distance] = (distances[e.distance] || 0) + 1;
});
or
var distances = {};
$.each(electrons,function(i,e) {
distances[this.distance] = (distances[this.distance] || 0) + 1;
});
Also may I point out to you that although this code good to look and pact, this is not generally faster. Better make your code more faster and more easy to look at as:
var distances = {},e;
for (var i = 0,l=electrons.length; i < l; i++) {
e = electrons[i];
distances[e.distance] = (distances[e.distance] || 0) + 1;
}
本文标签: jqueryJavaScript Get unique values and their counts from an array of objectsStack Overflow
版权声明:本文标题:jquery - JavaScript: Get unique values and their counts from an array of objects? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741414979a2377457.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论