[英]JS, JSON: How to get the n first items respecting 2 conditions?
Given data such : 给定的数据如下 :
var people = [
{ 'myKey': 'A', 'status': 0, score: 1.5 },
{ 'myKey': 'C', 'status': 1, score: 2.0 },
{ 'myKey': 'D', 'status': 0, score: 0.2 },
{ 'myKey': 'E', 'status': 1, score: 1.0 },
{ 'myKey': 'F', 'status': 0, score: 0.4 },
{ 'myKey': 'G', 'status': 1, score: 3.0 },
];
How to get all items with 'status':1
such 如何获得所有带有
'status':1
物品'status':1
这样
var people2= [
{ 'myKey': 'C', 'status': 1, score: 2.0 },
{ 'myKey': 'E', 'status': 1, score: 1.0 },
{ 'myKey': 'G', 'status': 1, score: 3.0 },
];
Edit: My final aim is to get the n=2 items with 'status':1
in ascending order, such: 编辑:我的最终目的是使n = 2项具有
'status':1
的升序,例如:
var people3= [
{ 'myKey': 'E', 'status': 1, score: 1.0 },
{ 'myKey': 'C', 'status': 1, score: 2.0 },
{ 'myKey': 'G', 'status': 1, score: 3.0 },
];
My approach is one function to get from var people
all items of 'status':1
into people2
(it's the code I'am asking right here), one fn to sort people2
by ascending score ( people3
), then one fn to pick 'myKey':
value of the n=2
first items. 我的方法是将
var people
所有'status':1
转换为people2
(这是我在这里要查询的代码),通过fn来对人people2
进行升序排序( people3
),然后再通过一个fn来选择'myKey':
n=2
第一项的值。 So I get 所以我得到
var people4 = [ 'E', 'C' ];
function getMyKeys(top) {
var result = people.filter(function (item) {
return item["status"] === 1; //only status=1
})
.sort(function (a, b) {
return a["score"] - b["score"]; //sort
})
.slice(0, top) //top n
.map(function (item) {
return item["myKey"]; //return "myKey" property only, if needed.
});
}
Use the new filter
method to conditionally reduce the set of items in the array. 使用新的
filter
方法有条件地减少数组中的项目集。 Once reduced sort the items by passing a comparison function to Array.sort()
一旦减少,就可以通过将比较函数传递给
Array.sort()
对项目进行排序
var people = [
{ 'myKey': 'A', 'status': 0, score: 1.5 },
{ 'myKey': 'C', 'status': 1, score: 2.0 },
{ 'myKey': 'D', 'status': 0, score: 0.2 },
{ 'myKey': 'E', 'status': 1, score: 1.0 },
{ 'myKey': 'F', 'status': 0, score: 0.4 },
{ 'myKey': 'G', 'status': 1, score: 3.0 },
];
var selected = people.filter(function(e){
return e.status == 1;
});
selected.sort(function(a,b){
if(a.score < b.score){return -1;}
if(a.score > b.score){ return 1;}
return 0;
});
If you must support older browsers you may need to build the .filter
method into the browser. 如果必须支持较旧的浏览器,则可能需要在浏览器中构建
.filter
方法。 This documentation on MDN contains a native implementation of .filter
that can be added to the browser. 有关MDN的文档包含
.filter
的本机实现,可以将其添加到浏览器中。
Working Example http://jsfiddle.net/5zt3A/ 工作示例 http://jsfiddle.net/5zt3A/
You'll have to fiter filter by status, then sort by score and then to map only myKey 您必须根据状态进行过滤,然后按分数排序,然后仅映射myKey
var people = [
{ 'myKey': 'A', 'status': 0, score: 1.5 },
{ 'myKey': 'C', 'status': 1, score: 2.0 },
{ 'myKey': 'D', 'status': 0, score: 0.2 },
{ 'myKey': 'E', 'status': 1, score: 1.0 },
{ 'myKey': 'F', 'status': 0, score: 0.4 },
{ 'myKey': 'G', 'status': 1, score: 3.0 },
];
var result = people.filter(function(i) {
return i.status == 1;
})
.sort(function (a, b) {
if (a.score == b.score) return 0;
if (a.score > b.score) return 1;
return -1;
}).map(function(i) {
return i.myKey;
});
Putting up with another set of answer, I prepared a Link that you can view for more details. 提出了另一组答案,我准备了一个链接 ,您可以查看该链接以获取更多详细信息。
(function getPeopleStatus (person){
for(var ctr = 0; ctr< person.length; ctr++){
if(person[ctr].status === 1){
selection.push(person[ctr]);
}
}
selection.sort()
})(people);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.