-
叮当猫咪
我们应该能够使用 Object.entries 和 Array.sort 按照我们的意愿排列对象,然后打印出结果:gladiators = { Pesho: { Duck: '400' }, Gladius: { Heal: '200', Support: '250', Shield: '250' }}// Get our array of gladiators, add total skill and add sorted abilities array.let result = Object.entries(gladiators).map(([name, glad]) => { let abilities = Object.entries(glad); return { name, Total: abilities.reduce((acc, [k,v]) => acc + Number(v) , 0), abilities: abilities.sort(([k1,v1], [k2,v2]) => v2 - v1) };});// Sort the result in descending order by total skill.result.sort((a,b) => b.Total - a.Total);// Print out our result.result.forEach(res => { console.log(`${res.name}: ${res.Total} skill`) res.abilities.forEach(([k,v]) => console.log(` - ${k} <!>`,v));})
-
尚方宝剑之说
我会把你的计算分成一个单独的数组,并在你输出之前对这个新数组进行排序。例如:const gladiators = { Pesho: { Duck: '400' }, Gladius: { Heal: '200', Support: '250', Shield: '250' }}const valueDescNameAsc = (a, b) => { if (a.value === b.value) return a.name.localeCompare(b.name) return a.value > b.value ? -1 : 1}// Transform data into arrays of objects (and calculate top-level value)const gladiatorSkills = Object.entries(gladiators) .map(([name, skills]) => ({ name, value: Object.values(skills).map(Number).reduce((a, b) => a + b, 0), skills: Object.entries(skills).map(([name, value]) => ({ name, value })).sort(valueDescNameAsc) })) .sort(valueDescNameAsc)// Output the transformed gladiatorSkills datafor (let { name, value, skills } of gladiatorSkills) { console.log(`${name}: ${value} skill`) for (let { name, value } of skills) { console.log(`- ${name} <!> ${value}`) }}
-
哆啦的时光机
对象不是按照顺序构建的。这是数组的工作。查看您的数据,拥有一个可以使用数组方法(例如排序)进行操作的角斗士列表会更合适。因此,在下面的示例中,我将您的数据重写为一个对象数组,其中包含嵌套数组。这将保证订单将得到兑现。它还将使代码的操作不那么复杂,因为您只是在处理数组,不需要转换。map,reduce和的组合 sort将带您到达您需要去的地方。const gladiators = [ { name: 'Pesho', stats: [ { name: 'Duck', value: 400 } ], }, { name: 'Gladius', stats: [ { name: 'Heal', value: 200 }, { name: 'Support', value: 300 }, { name: 'Shield', value: 250 } ], },];gladiators /** * Calculate the total skill and * sort the skills based on their value. */ .map(({ name, stats }) => { const skill = stats.reduce((acc, { value }) => acc + value, 0); const sortedStats = stats.sort((a, b) => b.value - a.value); return { name, skill, stats: sortedStats }; }) /** * Sort the gladiators by skill (desc) */ .sort((a, b) => b.skill - a.skill) /** * Print the data */ .forEach((gladiator) => { console.log(`${gladiator.name}: ${gladiator.skill}`) gladiator.stats.forEach(({ name, value }) => { console.log(`- ${name} <!> ${value}`); }); });
-
繁花如伊
我会通过创建一个新列表来简化这个问题,以总技能水平为索引,然后对该列表进行排序,并记录每个条目;注意:这段代码可以进一步简化,但我已经这样发布了,所以步骤对 OP 来说很清楚;// Original dataconst data = { Pesho: { Duck: '400' }, Gladius: { Heal: '200', Support: '250', Shield: '250' } };// Index each gladiator on total skilllet indexOnTotal = {};for (let obj of Object.entries(data)) { // Get obj value's const [name, skills] = obj; // Total Skill count const total = Object.keys(skills).reduce((sum,key)=>sum+parseFloat(skills[key]||0),0); // Add indexOnTotal[total] = obj;}// Sortlet sorted = Object.keys(indexOnTotal).sort().reverse().map(key=> ({...indexOnTotal[key],key:key}) );// Logfor (var g in sorted) { // Original gladiator const [ name, skills, total ] = Object.values(sorted[g]); // Name + Total console.log(`${name} (${total} skill)`); // Skills for (const [name, value] of Object.entries(skills)) { console.log(`${name}: ${value}`); }}输出;Gladius (700 skill)Heal: 200Support: 250Shield: 250Pesho (400 skill)Duck: 400