我有这个排序数组,我想根据点添加位置值。如果点数相同,则位置相同

时间:2018-01-20 16:42:36

标签: javascript arrays sorting logic

我得到了错误的结果我已经干了几次,我不知道我在哪里滞后。我首先对这个数组进行了排序,现在我想从这个数组中创建一个数组,其中包含position属性。这将具有基于用户的点的用户位置。



var ranking_dayArr = [ { "key": "5K5NODEywoYj2wztGZQfkiZui0I2", "points_neg": -3 }, { "key": "bIluKNDpcuVvG2cIOILVNIm3wd03", "points_neg": -3 }, { "key": "ix7DniFJKsdEQ4oDvs0AQPMkTf82", "points_neg": -3 }, { "key": "b79GsKX9weN5lZOt0RWHoS6lP002", "points_neg":-3 }, { "key": "qnGZ7N5OLldg553fA5qVwQIMfa83", "points_neg": -3 }, { "key": "w4685dkjS4RAr6YFzVrV4gVrF603", "points_neg": -3 }, { "key": "wY1aixbDcTTimzLLPYsOsjD5GnO2", "points_neg": -3 }, { "key": "Ga71YJ861QVixQ0AugqfVfAcDqw1", "points_neg": -3 }, {"key": "gI9VIXQEqlUJWChZ20zS92FAtkl1", "points_neg": -3 }, { "key": "Uez7dbef9QciikxVtffEGg3ynYx2", "points_neg": -3 }, { "key": "SERnmqrduSejqSgDzvILJAZ0cKu1", "points_neg": 0 }, { "key": "SPSqJcCdKPhmDqv2QyB66ahLkrr2", "points_neg": 0 }, { "key": "rrKtq0KOCGeCIDAkWHBZUxsx5zF2","points_neg": 0 }, { "key": "4vaF6BVxoDeaKBsDqFb6yGnbhOi2", "points_neg": 0 } ];
var positions = [], position= 1;
for(var i = 0 ; i < ranking_dayArr.length ; i++){
  // console.log(ranking_dayArr[i]);
  // console.log(ranking_dayArr[i].key);
  if(i === 0){
    positions.push({
      key:ranking_dayArr[i].key,
      position : {position:position, position_neg:(position * -1)}
    });
    position = position + 1; 
  }else if(ranking_dayArr[i-1].points_neg === ranking_dayArr[i].points_neg){
    positions.push({
      key:ranking_dayArr[i].key,
      position : {position:position, position_neg:(position * -1)}
    });
  }else{
    positions.push({
      key:ranking_dayArr[i].key,
      position : {position:position, position_neg:(position * -1)}
    })
    position = position + 1;   
  }
} 
console.log(ranking_dayArr);
console.log(positions);
&#13;
&#13;
&#13;

请运行此代码并帮助我,如果2个用户拥有相同的积分,我需要根据用户的用户点添加位置,然后他们将拥有相同的位置。

2 个答案:

答案 0 :(得分:1)

您可以检查数组中对象的前导,如果它们的points_neg值不同,则增加等级。

var data = [{ key: "5K5NODEywoYj2wztGZQfkiZui0I2", points_neg: -3 }, { key: "bIluKNDpcuVvG2cIOILVNIm3wd03", points_neg: -3 }, { key: "ix7DniFJKsdEQ4oDvs0AQPMkTf82", points_neg: -3 }, { key: "b79GsKX9weN5lZOt0RWHoS6lP002", points_neg: -3 }, { key: "qnGZ7N5OLldg553fA5qVwQIMfa83", points_neg: -3 }, { key: "w4685dkjS4RAr6YFzVrV4gVrF603", points_neg: -3 }, { key: "wY1aixbDcTTimzLLPYsOsjD5GnO2", points_neg: -3 }, { key: "Ga71YJ861QVixQ0AugqfVfAcDqw1", points_neg: -3 }, { key: "gI9VIXQEqlUJWChZ20zS92FAtkl1", points_neg: -3 }, { key: "Uez7dbef9QciikxVtffEGg3ynYx2", points_neg: -3 }, { key: "SERnmqrduSejqSgDzvILJAZ0cKu1", points_neg: 0 }, { key: "SPSqJcCdKPhmDqv2QyB66ahLkrr2", points_neg: 0 }, { key: "rrKtq0KOCGeCIDAkWHBZUxsx5zF2", points_neg: 0 }, { key: "4vaF6BVxoDeaKBsDqFb6yGnbhOi2", points_neg: 0 }],
    position = 0,
    result = data.map((o, i, a) =>
        Object.assign({}, o, {
           position: o.points_neg === (a[i - 1] || {}).points_neg ? position : ++position
        }));
   
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:1)

您的代码很好,您只需在第一个position = position + 1;条件中对if进行评论。

var ranking_dayArr = [ { "key": "5K5NODEywoYj2wztGZQfkiZui0I2", "points_neg": -3 }, { "key": "bIluKNDpcuVvG2cIOILVNIm3wd03", "points_neg": -3 }, { "key": "ix7DniFJKsdEQ4oDvs0AQPMkTf82", "points_neg": -3 }, { "key": "b79GsKX9weN5lZOt0RWHoS6lP002", "points_neg":-3 }, { "key": "qnGZ7N5OLldg553fA5qVwQIMfa83", "points_neg": -3 }, { "key": "w4685dkjS4RAr6YFzVrV4gVrF603", "points_neg": -3 }, { "key": "wY1aixbDcTTimzLLPYsOsjD5GnO2", "points_neg": -3 }, { "key": "Ga71YJ861QVixQ0AugqfVfAcDqw1", "points_neg": -3 }, {"key": "gI9VIXQEqlUJWChZ20zS92FAtkl1", "points_neg": -3 }, { "key": "Uez7dbef9QciikxVtffEGg3ynYx2", "points_neg": -3 }, { "key": "SERnmqrduSejqSgDzvILJAZ0cKu1", "points_neg": 0 }, { "key": "SPSqJcCdKPhmDqv2QyB66ahLkrr2", "points_neg": 0 }, { "key": "rrKtq0KOCGeCIDAkWHBZUxsx5zF2","points_neg": 0 }, { "key": "4vaF6BVxoDeaKBsDqFb6yGnbhOi2", "points_neg": 0 } ];
var positions = [], position= 1;
for(var i = 0 ; i < ranking_dayArr.length ; i++){
  // console.log(ranking_dayArr[i]);
  // console.log(ranking_dayArr[i].key);
  if(i === 0){
    positions.push({
      key:ranking_dayArr[i].key,
      position : {position:position, position_neg:ranking_dayArr[i].points_neg}
    });
    //comment this line
    //position = position + 1; 
  }else if(ranking_dayArr[i-1].points_neg === ranking_dayArr[i].points_neg){
    positions.push({
      key:ranking_dayArr[i].key,
      position : {position:position, position_neg:ranking_dayArr[i].points_neg}
    });
  }else{
    position = position + 1;
    positions.push({
      key:ranking_dayArr[i].key,
      position : {position:position, position_neg:ranking_dayArr[i].points_neg}
    });   
  }
} 
console.log(ranking_dayArr);
console.log(positions);