简体   繁体   中英

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

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:

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. 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.
       });
   }

FIDDLE DEMO

Use the new filter method to conditionally reduce the set of items in the array. Once reduced sort the items by passing a comparison function to 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. This documentation on MDN contains a native implementation of .filter that can be added to the browser.

Working Example http://jsfiddle.net/5zt3A/

You'll have to fiter filter by status, then sort by score and then to map only 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;
    });

http://jsfiddle.net/GrYuK/1/

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM