简体   繁体   English

如何将 position 添加到 javascript / Vue.js 中的现有阵列?

[英]How to add position in order to existing array in javascript / Vue.js?

I have array of players sorted by number of goals:我有一系列按进球数排序的球员:

 let players = [
  {"name": "player1", "goals": "5"},
  {"name": "player5", "goals": "4"},
  {"name": "player2", "goals": "4"},
  {"name": "player3", "goals": "2"},
  {"name": "player4", "goals": "1"}
]

I want to show this data with the position in the table, like this:我想用表格中的 position 显示这些数据,如下所示:

  1. player1 - 5 goals球员1 - 5个进球
  2. player5 - 4 goals球员 5 - 4 球
  3. player2 - 4 goals球员2 - 4个进球
  4. player3 - 2 goals球员 3 - 2 球
  5. player4 - 1 goal球员 4 - 1 球

If two (or more players) have the same number of goals - they must have the same position in the table (in example - 2.), end next number in enumartion should be missed (no number 3. in this example).如果两个(或更多球员)有相同数量的进球 - 他们必须在表中具有相同的 position(在示例中 - 2.),则应该错过枚举中的下一个数字(在此示例中没有数字 3.)。

How to add this type,,position in order in array'' (i'm not sure that's good words to describe this)?如何按数组顺序添加这种类型,position''(我不确定这是描述这个的好词)?

You could use an ordered list element ( <ol> ) to render that list, which automatically numbers the list items:您可以使用有序列表元素( <ol> )来呈现该列表,它会自动为列表项编号:

<ol>
  <li v-for="player in players" :key="player.name">
    {{ player.name }} - {{ player.goals }} goals
  </li>
</ol>

demo演示

Try this:尝试这个:

const
    player_obj = [
        { "name": "player1", "goals": "5" },
        { "name": "player5", "goals": "4" },
        { "name": "player2", "goals": "4" },
        { "name": "player3", "goals": "2" },
        { "name": "player4", "goals": "1" }
    ]

player_obj.sort((a, b) => a.goals - b.goals)

str = ''
for (let i = 0; i < player_obj.length; i++) {
    const
        player = player_obj[i]
    str += `
        ${i+1}. ${player.name} -  ${player.goal} goals
    `
}

table.innerHTML = str

You can do it like this:你可以这样做:

const players=[  {"name": "player1", "goals": "5"},
        {"name": "player5", "goals": "4"},
        {"name": "player2", "goals": "4"},
        {"name": "player3", "goals": "2"},
        {"name": "player4", "goals": "1"}]
const playersSorted = players.sort((a, b)=> a.goals - b.goals);
//console.log(playersSorted)
let currentPosition = 1; let lastGoalsNbr =playersSorted[0].goals;
const playersPositioned =  playersSorted.map(({name, goals})=> {
    if(lastGoalsNbr !== goals ) currentPosition ++;
  lastGoalsNbr = goals;
    return {name, goals, position:currentPosition}
  
  }
)

console.log(playersPositioned)

I required something similar to this some time ago and came up with this:前段时间我需要类似的东西并想出了这个:

function sortedRank(arr, childProp, asc) {
  let prev, position = 0, ranking = 0;
  return [...arr]
    .sort((a, b) => asc ? a[childProp] - b[childProp] : b[childProp] - a[childProp])
    .map((target, idx) => {
      const obj = { target };
      obj.indexRank = idx + 1;
      if (target[childProp] != prev) {
        position = obj.rank = obj.indexRank;
        ranking++;
        prev = target[childProp];
      } else {
        obj.rank = position;
      }
      obj.altRank = ranking;
      return obj
    });
}

It returns 3 different ranking types together with the child object from the original array.它返回 3 种不同的排名类型以及来自原始数组的子 object。

Where resultArr[0].rank is the rank from 1-N, but skips equal rank numbers.其中resultArr[0].rank是 1-N 的排名,但会跳过相等的排名数字。 For example:例如:

source  = resultArr[index].rank
goals 5 = 1.
goals 4 = 2.
goals 4 = 2.
goals 3 = 4.
goals 1 = 5.

resultArr[0].altRank doesn't skip ranking numbers. resultArr[0].altRank不会跳过排名数字。

source  = resultArr[index].altRank
goals 5 = 1.
goals 4 = 2.
goals 4 = 2.
goals 3 = 3.
goals 1 = 4.

and indexRank is the position after sorting.而indexRank是排序后的position。

 const list = [ {"name": "player1","goals": "5"}, {"name": "player5","goals": "4"}, {"name": "player2","goals": "4"}, {"name": "player3","goals": "2"}, {"name": "player4","goals": "1"} ]; function sortedRank(arr, childProp, ascending) { let prev, position = 0, ranking = 0; return [...arr].sort((a, b) => ascending? a[childProp] - b[childProp]: b[childProp] - a[childProp]).map((target, idx) => { const obj = { target }; obj.indexRank = idx + 1; if (target[childProp].= prev) { position = obj.rank = obj;indexRank; ranking++; prev = target[childProp]. } else { obj;rank = position. } obj;altRank = ranking; return obj }), } sortedRank(list. 'goals'),forEach(({ indexRank, rank, altRank. target }) => { console:log(`idxRank: ${indexRank} rank: ${rank} alternative, ${altRank}`; target); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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