javascript从数据中获取类似sql的DISTINCT计数


data = [
{
"index": 0,
"id": 47,
"sepallengthcm": 5.1,
"sepalwidthcm": 3.8,
"unnamed:_3": 1.6,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 1,
"id": 48,
"sepallengthcm": 4.6,
"sepalwidthcm": 3.2,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 2,
"id": 49,
"sepallengthcm": 5.3,
"sepalwidthcm": 3.7,
"unnamed:_3": 1.5,
"petalwidthcm": 0.2,
"species": "jennifer"
},
{
"index": 3,
"id": 50,
"sepallengthcm": 5.0,
"sepalwidthcm": 3.3,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 4,
"id": 97,
"sepallengthcm": 12.0,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.2,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 5,
"id": 98,
"sepallengthcm": 6.2,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.3,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 6,
"id": 99,
"sepallengthcm": 5.1,
"sepalwidthcm": 2.5,
"unnamed:_3": 3.0,
"petalwidthcm": 1.1,
"species": "kajol"
},
{
"index": 7,
"id": 100,
"sepallengthcm": 11.0,
"sepalwidthcm": 2.8,
"unnamed:_3": 7.0,
"petalwidthcm": 1.3,
"species": "floaw"
},
{
"index": 8,
"id": 101,
"sepallengthcm": 6.3,
"sepalwidthcm": 3.3,
"unnamed:_3": 6.0,
"petalwidthcm": 2.5,
"species": "Iris-flower"
},
{
"index": 9,
"id": 102,
"sepallengthcm": 5.8,
"sepalwidthcm": 2.7,
"unnamed:_3": 5.1,
"petalwidthcm": 1.9,
"species": "Iris-flower"
}
]

这是我的输入数据。我正试图使用专用字段来实现这些数据的提取计数

result=distictCount("species"(

result = [
{
"species": "Iris-flower",
"sepallengthcm": 2
},
{
"species": "floaw",
"sepallengthcm": 1
},
{
"species": "jennifer",
"sepallengthcm": 3
},
{
"species": "kajol",
"sepallengthcm": 1
},
{
"species": "setosa",
"sepallengthcm": 3
}
]

SELECT species, COUNT(DISTINCT sepallengthcm) as sepallengthcm FROM soubhagyairis GROUP BY species;

我的sql查询完成了这项工作。但是,我想使用javascript来实现这一点。请看一看。

我们怎样才能做到

感谢

我的sql查询完成了这项工作。但是,我想使用javascript来实现这一点。请看一看。

我们怎样才能做到

感谢

您可以使用for循环:

代码段:

var data = [
{
"index": 0,
"id": 47,
"sepallengthcm": 5.1,
"sepalwidthcm": 3.8,
"unnamed:_3": 1.6,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 1,
"id": 48,
"sepallengthcm": 4.6,
"sepalwidthcm": 3.2,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 2,
"id": 49,
"sepallengthcm": 5.3,
"sepalwidthcm": 3.7,
"unnamed:_3": 1.5,
"petalwidthcm": 0.2,
"species": "jennifer"
},
{
"index": 3,
"id": 50,
"sepallengthcm": 5.0,
"sepalwidthcm": 3.3,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 4,
"id": 97,
"sepallengthcm": 12.0,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.2,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 5,
"id": 98,
"sepallengthcm": 6.2,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.3,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 6,
"id": 99,
"sepallengthcm": 5.1,
"sepalwidthcm": 2.5,
"unnamed:_3": 3.0,
"petalwidthcm": 1.1,
"species": "kajol"
},
{
"index": 7,
"id": 100,
"sepallengthcm": 11.0,
"sepalwidthcm": 2.8,
"unnamed:_3": 7.0,
"petalwidthcm": 1.3,
"species": "floaw"
},
{
"index": 8,
"id": 101,
"sepallengthcm": 6.3,
"sepalwidthcm": 3.3,
"unnamed:_3": 6.0,
"petalwidthcm": 2.5,
"species": "Iris-flower"
},
{
"index": 9,
"id": 102,
"sepallengthcm": 5.8,
"sepalwidthcm": 2.7,
"unnamed:_3": 5.1,
"petalwidthcm": 1.9,
"species": "Iris-flower"
}
];

var result = [];
var intermediate = [];
for (var i = 0; i < data.length; i++) {
intermediate[i] = data[i].species;
}
intermediate.sort();
var n = 0;
for (var i = 0; i <= intermediate.length; i++) {

if (intermediate[i] !== intermediate[i - 1]) {
if (intermediate[i - 1] !== undefined) {
result.push({"flower": intermediate[i-1], "n": n});
}
n = 1;
} else {
n++;
}
}
console.log(result);

代码:

var result = [];
var intermediate = [];
for (var i = 0; i < data.length; i++) {
intermediate[i] = data[i].species;
}
intermediate.sort();
var n = 0;
for (var i = 0; i <= intermediate.length; i++) {

if (intermediate[i] !== intermediate[i - 1]) {
if (intermediate[i - 1] !== undefined) {
result.push({"flower": intermediate[i-1], "n": n});
}
n = 1;
} else {
n++;
}
}
console.log(result);

你可以试试这个:

const distinctCount = (data, group, key) => {
return Array.from(new Set(data.map(d => d[group])))
.map(g => {return {
[group]: g,
[key]: data.filter(d => d[group] == g).length
}});
}

使用new Set(data.map(d => d[group]))生成一个Set,它为您完成SQL DISTINCT的艰苦工作,并使用Array.from(etc)转换为一个常规的Javascript数组。

然后,.map(...)为每个DISTINCT物种创建一个新对象,并使用data.filter(d => d[group] == g).length找到计数

data = [
{
"index": 0,
"id": 47,
"sepallengthcm": 5.1,
"sepalwidthcm": 3.8,
"unnamed:_3": 1.6,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 1,
"id": 48,
"sepallengthcm": 4.6,
"sepalwidthcm": 3.2,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 2,
"id": 49,
"sepallengthcm": 5.3,
"sepalwidthcm": 3.7,
"unnamed:_3": 1.5,
"petalwidthcm": 0.2,
"species": "jennifer"
},
{
"index": 3,
"id": 50,
"sepallengthcm": 5.0,
"sepalwidthcm": 3.3,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 4,
"id": 97,
"sepallengthcm": 12.0,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.2,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 5,
"id": 98,
"sepallengthcm": 6.2,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.3,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 6,
"id": 99,
"sepallengthcm": 5.1,
"sepalwidthcm": 2.5,
"unnamed:_3": 3.0,
"petalwidthcm": 1.1,
"species": "kajol"
},
{
"index": 7,
"id": 100,
"sepallengthcm": 11.0,
"sepalwidthcm": 2.8,
"unnamed:_3": 7.0,
"petalwidthcm": 1.3,
"species": "floaw"
},
{
"index": 8,
"id": 101,
"sepallengthcm": 6.3,
"sepalwidthcm": 3.3,
"unnamed:_3": 6.0,
"petalwidthcm": 2.5,
"species": "Iris-flower"
},
{
"index": 9,
"id": 102,
"sepallengthcm": 5.8,
"sepalwidthcm": 2.7,
"unnamed:_3": 5.1,
"petalwidthcm": 1.9,
"species": "Iris-flower"
}
];
const distinctCount = (data, group, key) => {
return Array.from(new Set(data.map(d => d[group])))
.map(g => {return {
[group]: g,
[key]: data.filter(d => d[group] == g).length
}});
}
console.log(distinctCount(data, "species", "sepallengthcm"));

我使用了underscore.js countBy方法。

data = [
{
"index": 0,
"id": 47,
"sepallengthcm": 5.1,
"sepalwidthcm": 3.8,
"unnamed:_3": 1.6,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 1,
"id": 48,
"sepallengthcm": 4.6,
"sepalwidthcm": 3.2,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 2,
"id": 49,
"sepallengthcm": 5.3,
"sepalwidthcm": 3.7,
"unnamed:_3": 1.5,
"petalwidthcm": 0.2,
"species": "jennifer"
},
{
"index": 3,
"id": 50,
"sepallengthcm": 5.0,
"sepalwidthcm": 3.3,
"unnamed:_3": 1.4,
"petalwidthcm": 0.2,
"species": "setosa"
},
{
"index": 4,
"id": 97,
"sepallengthcm": 12.0,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.2,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 5,
"id": 98,
"sepallengthcm": 6.2,
"sepalwidthcm": 2.9,
"unnamed:_3": 4.3,
"petalwidthcm": 1.3,
"species": "jennifer"
},
{
"index": 6,
"id": 99,
"sepallengthcm": 5.1,
"sepalwidthcm": 2.5,
"unnamed:_3": 3.0,
"petalwidthcm": 1.1,
"species": "kajol"
},
{
"index": 7,
"id": 100,
"sepallengthcm": 11.0,
"sepalwidthcm": 2.8,
"unnamed:_3": 7.0,
"petalwidthcm": 1.3,
"species": "floaw"
},
{
"index": 8,
"id": 101,
"sepallengthcm": 6.3,
"sepalwidthcm": 3.3,
"unnamed:_3": 6.0,
"petalwidthcm": 2.5,
"species": "Iris-flower"
},
{
"index": 9,
"id": 102,
"sepallengthcm": 5.8,
"sepalwidthcm": 2.7,
"unnamed:_3": 5.1,
"petalwidthcm": 1.9,
"species": "Iris-flower"
}
];
let a = _.countBy(data, function(item) {
return item.species;
});
tmp = [];
for (const key in a) {
val = {}
val.species = key;
val.sepallengthcm = a[key]

tmp.push(val);
}
console.log(tmp);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.12.0/underscore-min.js" integrity="sha512-BDXGXSvYeLxaldQeYJZVWXJmkisgMlECofWFXKpWwXnfcp/R708nrs/BtNLH5cb/5TE7aeYRTDBRXu6kRL4VeQ==" crossorigin="anonymous"></script>

示例https://jsfiddle.net/8Lph7503/

来源:https://underscorejs.org/#countBy

最新更新