Javascript 对对象进行排序,但每个键都是唯一的

问题描述 投票:0回答:1

对于国际象棋应用程序,我需要根据值对该对象的元素进行排序,在 javascript 中:

const Moves = {e7e5: '-42', g8f6: '-16', h7h6: '-38', c8d7: '-29'};
javascript sorting object
1个回答
0
投票
var Moves = {e7e5: '-42', g8f6: '-16', h7h6: '-38', c8d7: '-29'};

//Convert to array
Moves=Object.entries(Moves);

//Sort
Moves.sort((x, y)=>x[1]-y[1]);

//Convert back to an object
Moves=Object.fromEntries(sortedObject);
© www.soinside.com 2019 - 2024. All rights reserved.