英文名排序
作者:互联网
const data = [ { attributes: { firstName: 'Ryan', lastName: 'Karsa', }, }, { attributes: { firstName: 'Carol', lastName: 'Filippelli', }, }, { attributes: { firstName: 'Ryan', lastName: 'Hudson', }, }, { attributes: { firstName: 'Ryan', lastName: 'Lewis', }, }, { attributes: { firstName: 'Sparky', lastName: 'Malone', }, }, { attributes: { firstName: 'Sparky', lastName: 'Bond', }, }, { attributes: { firstName: 'Andy', lastName: 'Bernard', }, }, { attributes: { firstName: 'Kevin', lastName: 'Leonardo', }, }, { attributes: { firstName: 'Erin', lastName: 'Hannon', }, }, { attributes: { firstName: 'Oscar', lastName: 'Howard', }, }, { attributes: { firstName: 'Erin', lastName: 'Perkz', }, }, { attributes: { firstName: 'David', lastName: 'Wallace', }, }, { attributes: { firstName: 'Karen', lastName: 'Filippelli', }, }, { attributes: { firstName: 'Omar', lastName: 'Abbott', }, }, { attributes: { firstName: 'Gabe', lastName: 'Lewis', }, }, { attributes: { firstName: 'Sparky', lastName: 'Allam', }, }, { attributes: { firstName: 'Holly', lastName: 'Flax', }, }, { attributes: { firstName: 'Jan', lastName: 'Filippelli', }, }, { attributes: { firstName: 'Jan', lastName: 'Levinson', }, }, { attributes: { firstName: 'David', lastName: 'Levinson', }, }, { attributes: { firstName: 'Ryan', lastName: 'Howard', }, }, { attributes: { firstName: 'Lindsey', lastName: 'Perez', }, }, { attributes: { firstName: 'James', lastName: 'Bond', }, }, { attributes: { firstName: 'Meredith', lastName: 'Palmer', }, }, { attributes: { firstName: 'Kevin', lastName: 'Malone', }, }, { attributes: { firstName: 'David', lastName: 'Anne', }, }, { attributes: { firstName: 'Stanley', lastName: 'Hudson', }, }, { attributes: { firstName: 'Hathaway', lastName: 'Karen', }, }, { attributes: { firstName: 'Oscar', lastName: 'Martinez', }, }, { attributes: { firstName: 'Frank', lastName: 'Wright', }, }, { attributes: { firstName: 'Charles', lastName: 'Miner', }, }, { attributes: { firstName: 'Carol', lastName: 'Stills', }, }, ];let alphabetArr = data .map(item => item.attributes.lastName) .sort() .map((item, index) => ({ index, name: item }));
data.sort((a, b) => { return ( alphabetArr.find(item => item.name === a.attributes.lastName).index - alphabetArr.find(item => item.name === b.attributes.lastName).inde ); });
标签:Sparky,firstName,lastName,Ryan,item,英文名,attributes,排序 来源: https://www.cnblogs.com/it-Ren/p/15711631.html